Building Char Utilities
Practical examples.
Building Char Utilities is a free C Academy lesson on CoddyKit. This is lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 4 lessons in total.
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;
}Reversing a String
Swap characters from the two ends moving inward to reverse a string in place.
#include <stdio.h>
#include <string.h>
int main(void) {
char s[] = "hello";
int n = strlen(s);
for (int i = 0; i < n / 2; i++) {
char tmp = s[i];
s[i] = s[n-1-i];
s[n-1-i] = tmp;
}
printf("%s\n", s);
return 0;
}Checking a Palindrome
A palindrome reads the same forwards and backwards. Compare mirrored positions until they meet.
#include <stdio.h>
#include <string.h>
int is_palindrome(const char *s) {
int i = 0, j = strlen(s) - 1;
while (i < j) {
if (s[i] != s[j]) return 0;
i++; j--;
}
return 1;
}
int main(void) {
printf("%d\n", is_palindrome("level"));
printf("%d\n", is_palindrome("world"));
return 0;
}Toggling Case
A case toggler makes uppercase lowercase and vice versa, leaving non-letters alone.
#include <stdio.h>
#include <ctype.h>
int main(void) {
char s[] = "Hello World";
for (int i = 0; s[i]; i++) {
if (isupper(s[i])) s[i] = tolower(s[i]);
else if (islower(s[i])) s[i] = toupper(s[i]);
}
printf("%s\n", s);
return 0;
}Stripping Non-Digits
Keep only digit characters from a string, useful for cleaning phone numbers.
#include <stdio.h>
#include <ctype.h>
int main(void) {
char *s = "(555) 123-4567";
for (int i = 0; s[i]; i++) {
if (isdigit((unsigned char)s[i])) putchar(s[i]);
}
putchar('\n');
return 0;
}Manual atoi
Convert a numeric string to an integer by accumulating each digit: multiply the running total by 10 and add the digit value.
#include <stdio.h>
#include <ctype.h>
int my_atoi(const char *s) {
int n = 0;
for (int i = 0; isdigit((unsigned char)s[i]); i++) {
n = n * 10 + (s[i] - '0');
}
return n;
}
int main(void) {
printf("%d\n", my_atoi("427") + 3);
return 0;
}Counting Words
Count words by detecting transitions from whitespace into a non-whitespace character.
#include <stdio.h>
#include <ctype.h>
int main(void) {
char *s = "the quick brown fox";
int words = 0, in_word = 0;
for (int i = 0; s[i]; i++) {
if (isspace((unsigned char)s[i])) in_word = 0;
else if (!in_word) { in_word = 1; words++; }
}
printf("words = %d\n", words);
return 0;
}Capitalizing Words
Make the first letter of each word uppercase by uppercasing any letter that follows whitespace or starts the string.
#include <stdio.h>
#include <ctype.h>
int main(void) {
char s[] = "hello there friend";
int start = 1;
for (int i = 0; s[i]; i++) {
if (isspace((unsigned char)s[i])) start = 1;
else { if (start) s[i] = toupper(s[i]); start = 0; }
}
printf("%s\n", s);
return 0;
}A ROT13 Cipher
ROT13 shifts each letter by 13 places, wrapping around the alphabet. Applying it twice restores the original.
#include <stdio.h>
#include <ctype.h>
int main(void) {
char s[] = "Hello";
for (int i = 0; s[i]; i++) {
char c = s[i];
if (isupper(c)) s[i] = 'A' + (c - 'A' + 13) % 26;
else if (islower(c)) s[i] = 'a' + (c - 'a' + 13) % 26;
}
printf("%s\n", s);
return 0;
}Frequency of a Character
Count how often a specific character appears in a string with a simple loop and comparison.
#include <stdio.h>
int main(void) {
char *s = "mississippi";
char target = 's';
int count = 0;
for (int i = 0; s[i]; i++) {
if (s[i] == target) count++;
}
printf("'%c' appears %d times\n", target, count);
return 0;
}Quick Check
Test your character utility skills.
Recap
You built practical character utilities:
- Counting vowels, words, and character frequency.
- Reversing strings and checking palindromes.
- Toggling and capitalizing case.
- Implementing a manual
atoiand a ROT13 cipher.
Learn C with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 39
- Lessons
- 144
Frequently Asked Questions
Is the “Building Char Utilities” lesson free?
Yes — the full text of “Building Char Utilities” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 4 lessons in total.
What will I learn in “Building Char Utilities”?
Practical examples. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
How long does the “Building Char Utilities” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Characters as Integers
- The ctype Library
- Reading Characters
- Building Char Utilities