C++ bootcamp · Lab 13

Count the vowels

easyStrings10 minLesson: Strings

Read the question, write C++ on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read one word and print how many vowels (a, e, i, o, u) it contains.

Count both cases — lowercase each character before testing.

Input. One line: a word.

Output. One integer: the vowel count.

Examples

Example 1 — o, a, i.
Input
Programming
Output
3
Example 2 — y does not count here.
Input
sky
Output
0
Hint
  1. Loop over the characters with for (char ch : word).
  2. #include <cctype>, then std::tolower to normalise case before comparing.
Show correct code

Peek only after you have tried. You can still Check your own version.

#include <iostream>
#include <string>
#include <cctype>

int main() {
  std::string word;
  std::cin >> word;
  int count = 0;
  for (char ch : word) {
    char l = std::tolower((unsigned char)ch);
    if (l == 'a' || l == 'e' || l == 'i' || l == 'o' || l == 'u') count++;
  }
  std::cout << count << "\n";
  return 0;
}
main.cppC++17 · g++ · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.