Challenge: Count the Vowels
Completed
Read a whole line of text and print how many vowels (a e i o u, small or capital) it has.
Input
One line of text (it may contain spaces).
Output
One integer.
Example
Input
Hello World
Output
3
Editor
C++
Hints
Use getline(cin, line) because the text has spaces.
Turn each char into lower case with tolower before comparing.
Solution explanation
Lower-casing each letter first means you only have to check five letters instead of ten.
Show solution
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
string line;
getline(cin, line);
int count = 0;
for (char c : line) {
char lower = tolower(c);
if (lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower == 'u') {
count++;
}
}
cout << count << "\n";
return 0;
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module