C bootcamp · Lab 18

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. Read a word with scanf("%1023s", word).
  2. #include <ctype.h>, then tolower each character before comparing.
Show correct code

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

#include <stdio.h>
#include <ctype.h>

int main(void) {
  char word[1024];
  if (scanf("%1023s", word) != 1) { printf("0\n"); return 0; }
  int count = 0;
  for (int i = 0; word[i]; i++) {
    char l = tolower((unsigned char)word[i]);
    if (l == 'a' || l == 'e' || l == 'i' || l == 'o' || l == 'u') count++;
  }
  printf("%d\n", count);
  return 0;
}
main.cC17 · gcc · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.