Challenge: Title Case
Completed
Read one line of words separated by single spaces and print it with the first letter of every word in capitals and all other letters small.
Input
One line of text.
Output
The same line in Title Case.
Example
Input
hello world
Output
Hello World
Editor
C
Hints
A letter starts a word if it is the first character or comes after a space.
Use toupper for word starts and tolower for everything else.
Solution explanation
We look at the character before each letter to decide whether it begins a new word.
Show solution
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char line[201];
fgets(line, 201, stdin);
line[strcspn(line, "\n")] = '\0';
for (int i = 0; line[i] != '\0'; i++) {
if (i == 0 || line[i - 1] == ' ') {
line[i] = toupper(line[i]);
} else {
line[i] = tolower(line[i]);
}
}
printf("%s\n", line);
return 0;
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module