C++ Exercises: Count all the vowels in a given string
C++ String: Exercise-7 with Solution
Write a C++ program to count all the vowels in a given string.
Pictorial Presentation:
Sample Solution:
C++ Code :
#include <iostream>
using namespace std;
int Vowel_Count(string text) {
int ctr = 0;
for(int i = 0; i < int(text.size()); i++){
if (text[i] == 'a' || text[i] == 'e' || text[i] == 'i' || text[i] == 'o' || text[i] == 'u')
++ctr;
if (text[i] == 'A' || text[i] == 'E' || text[i] == 'I' || text[i] == 'O' || text[i] == 'U')
++ctr;
}
return ctr;
}
int main() {
cout << "Original string: eagerer, number of vowels -> " << Vowel_Count("eagerer") << endl;
cout << "\nOriginal string: eaglets, number of vowels -> " << Vowel_Count("eaglets") << endl;
cout << "\nOriginal string: w3resource, number of vowels -> " << Vowel_Count("w3resource") << endl;
cout << "\nOriginal string: After eagling the Road Hole on Thursday, he missed an 8-footer for birdie Friday., number of vowels -> ";
cout << Vowel_Count("After eagling the Road Hole on Thursday, he missed an 8-footer for birdie Friday.") << endl;
return 0;
}
Sample Output:
Original string: eagerer, number of vowels -> 4 Original string: eaglets, number of vowels -> 3 Original string: w3resource, number of vowels -> 4
Flowchart:
C++ Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a C++ program to check whether the characters e and g are separated by exactly 2 places anywhere in a given string at least once.
Next: Write a C++ program to count all the words in a given string.
What is the difficulty level of this exercise?