typestar

Character strings in C

Walking a null-terminated string one char at a time.

int vowel_count(const char *text) {
    int count = 0;
    for (int i = 0; text[i] != '\0'; i++) {
        char c = text[i];
        if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
            count++;
    }
    return count;
}

How it works

  1. The loop stops at the '\0' terminator.
  2. Each character is compared against the vowels.
  3. The count accumulates as it scans.

Keywords and builtins used here

The run, in numbers

Lines
9
Characters to type
197
Tokens
94
Three-star pace
90 tpm

At the three-star pace of 90 tokens a minute, this run takes about 63 seconds.

Type this snippet

Step 3 of 5 in Arrays & strings, step 24 of 35 in Language basics.

← Previous Next →