Building Char Utilities
Practical examples.
Combining What You Know
Now you can build small, practical character utilities by combining char arithmetic, the ctype library, and string loops.
Let us write a few common helpers.
#include <stdio.h>
#include <ctype.h>
int main(void) {
char c = 'g';
printf("Upper: %c\n", toupper(c));
return 0;
}Counting Vowels
A vowel counter checks each character against the set a, e, i, o, u in either case.
#include <stdio.h>
#include <ctype.h>
int count_vowels(const char *s) {
int n = 0;
for (int i = 0; s[i]; i++) {
char c = tolower(s[i]);
if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u') n++;
}
return n;
}
int main(void) {
printf("%d\n", count_vowels("Education"));
return 0;
}All lessons in this course
- Characters as Integers
- The ctype Library
- Reading Characters
- Building Char Utilities