toupper
Uppercasing Text
There is no standard toupper coreutil, but it is a classic C exercise that shows how character manipulation works at the ASCII level.
The task: print a string with every lowercase letter converted to uppercase. All other characters (spaces, digits, punctuation, newlines) pass through unchanged.
ASCII and Character Arithmetic
In ASCII, the lowercase letters a–z have codes 97–122. The uppercase letters A–Z have codes 65–90. The difference is exactly 32.
So to uppercase a character:
if (c >= 'a' && c <= 'z') c = c - 'a' + 'A';
Or equivalently: c -= 32 (since 'a' - 'A' == 32). The first form is clearer.
Your Implementation
void to_upper(const char *s) {
while (*s) {
char c = *s;
if (c >= 'a' && c <= 'z') c = c - 'a' + 'A';
putchar(c);
s++;
}
}
No library functions needed — just arithmetic on character codes.
The Real tr Command
You can actually do this in the shell with tr:
$ echo "hello" | tr 'a-z' 'A-Z'
HELLO
You will implement a simplified version of tr in the next lesson.
Your Task
Implement to_upper that converts lowercase letters to uppercase, passing everything else through unchanged.