C: Count vowels

Read one line of text and print how many vowels (a, e, i, o, u, in either case) it contains.

What to build

A walk through one line of text.

Requirements

  • Read one whole line with fgets.
  • Count the vowels, in either case.

Input

One line of up to 200 characters.

Output

The number of vowels.

Constraints

The line has up to 200 characters.

Example

Input:

Hello World

Output:

3

Why it works

fgets keeps the spaces that %s would stop at, and the loop can stop at the '\0'.

Examples

Example 1 · Input
Hello World
Expected output
3
Example 2 · Input
rhythm
Expected output
0

Hints

Hint 1

Read the line with fgets(line, sizeof line, stdin).

Hint 2

tolower from ctype.h lets you test both 'A' and 'a' at once.

Solution

Show one correct solution (try it yourself first)
#include <stdio.h>
#include <ctype.h>

int main(void) {
    char line[256];
    if (fgets(line, sizeof line, stdin) == NULL) line[0] = '\0';
    int count = 0;
    for (int i = 0; line[i] != '\0'; i++) {
        char c = tolower(line[i]);
        if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') count++;
    }
    printf("%d\n", count);
    return 0;
}

Lesson: Strings are char arrays →