Count Number of Vowels, Consonants and Spaces in a Sentence
Here you will get a C++ program to count Number of Vowels Consonants and Spaces in a Sentence. Here we will be using the switch case and while loop to implement this example.
Program code to Count Number of Vowels Consonants and Spaces in a Sentence
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | //C++ program to Count Number of Vowels, Consonants and spaces in a Sentence #include<iostream> using namespace std; int main() { char ch[100]; int i=0,vowel=0,consonant=0,space=0; cout<<"Enter a Sentence : "; gets(ch); while(ch[i]!='\0') { switch(ch[i]) { case 'a': vowel++;break; case 'e': vowel++;break; case 'i': vowel++;break; case 'o': vowel++;break; case 'u': vowel++;break; case 'A': vowel++;break; case 'E': vowel++;break; case 'I': vowel++;break; case 'O': vowel++;break; case 'U': vowel++;break; case ' ': space++;break; default: consonant++; break; } i++; } cout<<"\nNumber of Vowels are : "<<vowel; cout<<"\nNumber of Consonants are : "<<consonant; cout<<"\nNumber of Spaces are : "<<space; return 0; } |
Output
Enter an Alphabet to check vowel or consonant : Welcomes to CodeRevise
Number of Vowels are : 9
Number of Consonants are : 11
Number of Spaces are : 2
Check out our other C++ programming Examples