C Workshop · Count Vowels
Lesson C

Count Vowels

Walk each character. If it is a, e, i, o, or u (any case), add one.

What it is

You walk each letter of a word and count how many are vowels: a e i o u, upper or lower. Kodin has two: o and i. Consonants do not add to the count.

Approach

  1. Read the word.
  2. count = 0. For each s[i], test the five vowels.
  3. Print count.

Where this is used in the real world

Diagram

1

K

no

2

o

+1

3

d

no

4

i + n

+1

Run algorithm

Each step highlights the matching C line. Program output appears only when a line that prints actually runs.

Ready
C code
1#include <stdio.h>
2int main(void) {
3    char s[64];
4    int i, count = 0;
5    scanf("%63s", s);
6    for (i = 0; s[i] != 0; i++) {
7        char c = s[i];
8        if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||
9            c=='A'||c=='E'||c=='I'||c=='O'||c=='U') count++;
10    }
11    printf("%d\n", count);
12    return 0;
13}
What this line does
Press Run algorithm. The highlight will move through the C code.
Program output
(no output yet)

Check your understanding

Five questions. Pick one answer each, then Check answers. The correct choice turns green.

1. Kodin has how many vowels?

o and i.

2. The loop stops when:

Terminator.

3. Uppercase A is counted because:

Both cases.

4. This walk does not need strlen first because:

s[i] != 0.

5. A consonant increments count?

Only vowels.