Google Add

Search

C++ Program to Count the Number of Vowels and consonants in a String

Write a C++ program to count the number of vowels and consonants in a string. In this programming question, We have to write a code which takes a string as an input and count number of vowels and consonants in a string.

C program to count the number of vowels and consonants in a string

C++ Program to Count the Number of Vowels and consonants in a String



How to read string with spaces using scanf

Programming Questions on Strings

C program to find the length of string without using strlen


C++ Program to Count Number of Vowels and Consonants in a String


#include <iostream>
#include <string.h>

using namespace std;

int main() {
 
     char str[300];
     int  vowel = 0, consonant = 0;
 
     cout << "Enter a string \n";
     cin.getline(str, sizeof(str));

      /* Length of a string. */
    
      int len = strlen(str);
    
     /* Traverse a string */
     for(int i = 0; i < len; i++){
          
       /* Check for vowel and increment the count. */

       if( str[i]=='a' || str[i]=='A' 
           || str[i]=='e' || str[i]=='E' 
           || str[i]=='i' || str[i]=='I' 
           ||str[i]=='o' ||str[i]=='O' 
           ||str[i]=='u' ||str[i]=='U' ) {
             
                vowel++;
                
             /* Check for consonant. */

       } else if((str[i] >= 'a' && str[i] <= 'z') 
                  || (str[i] >= 'A' && str[i] <= 'Z')) {
             
               consonant++;
       }
     }
     
     cout << "Vowels = " << vowel << " Consonants = " << consonant;
 
    return 0;
}



Output -

Enter a string  - I love cprogrammingcode.com

Vowels = 9 and Consonants = 15


No comments:

Post a Comment