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
- The loop stops at the
'\0'terminator. - Each character is compared against the vowels.
- The count accumulates as it scans.
Keywords and builtins used here
charconstforifintreturnvowel_count
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.
Step 3 of 5 in Arrays & strings, step 24 of 35 in Language basics.