This program reads a sentence and swaps the case of every letter — uppercase becomes lowercase, lowercase becomes uppercase. Digits, spaces, and punctuation are left unchanged. The <ctype.h> functions isupper(), islower(), toupper(), and tolower() make this clean and portable across all ASCII and extended character sets.
The original post used conio.h, void main(), a broken getchar()!='n' loop (should be '\n'), and sentence[i]=' ' instead of the required null terminator '\0' — that last bug would cause undefined behavior when the string was read back. This rewrite uses fgets() for safe input and ctype.h functions for correct case detection.
C Program: Toggle Case of a String
/* Toggle case of every character in a string (lower↔upper)
* Compile: gcc -ansi -Wall -Wextra togglecase.c -o togglecase */
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(void)
{
char sentence[256];
int i;
printf("Enter a sentence: ");
if (fgets(sentence, sizeof(sentence), stdin) == NULL) return 1;
sentence[strcspn(sentence, "\n")] = '\0';
printf("Original : %s\n", sentence);
for (i = 0; sentence[i] != '\0'; i++) {
if (isupper((unsigned char)sentence[i]))
sentence[i] = (char)tolower((unsigned char)sentence[i]);
else if (islower((unsigned char)sentence[i]))
sentence[i] = (char)toupper((unsigned char)sentence[i]);
/* digits, spaces, punctuation are left unchanged */
}
printf("Toggled : %s\n", sentence);
return 0;
}
How to Compile and Run
gcc -ansi -Wall -Wextra togglecase.c -o togglecase
./togglecase
Sample Output
Enter a sentence: Hello, World! Original : Hello, World! Toggled : hELLO, wORLD! Enter a sentence: C Programming is FUN! Original : C Programming is FUN! Toggled : c pROGRAMMING IS fun!
Character-by-Character Trace (“Hello”)
| Index | Character | isupper? | islower? | Result |
|---|---|---|---|---|
| 0 | ‘H’ | Yes | No | tolower → ‘h’ |
| 1 | ‘e’ | No | Yes | toupper → ‘E’ |
| 2 | ‘l’ | No | Yes | toupper → ‘L’ |
| 3 | ‘l’ | No | Yes | toupper → ‘L’ |
| 4 | ‘o’ | No | Yes | toupper → ‘O’ |
| 5 | ‘,’ | No | No | unchanged |
| 6 | ‘ ‘ | No | No | unchanged |
Code Explanation
isupper((unsigned char)c)andislower((unsigned char)c)— the ctype.h functions take anintargument that must be in the range[0, 255]or equal toEOF. On platforms wherecharis signed, characters with values 128–255 become negative integers, which are out of range and trigger undefined behavior. Casting tounsigned charbefore passing to ctype.h functions is the correct and portable pattern.toupper()andtolower()— return the converted character as anint. Cast the result back tocharbefore storing in the string:sentence[i] = (char)tolower(...). The cast is safe because the return value is always a valid character code.- Digits and punctuation are unchanged — the else-if chain only modifies characters where isupper or islower is true. For ‘0’–’9′, ‘,’, ‘!’, ‘ ‘, etc., neither condition fires, so the character passes through the loop unmodified.
- fgets + strcspn for safe input —
fgets(sentence, sizeof(sentence), stdin)prevents buffer overflow.sentence[strcspn(sentence, "\n")] = '\0'strips the trailing newline that fgets includes, giving a clean string to process.
What This Program Teaches
- Correct ctype.h usage requires unsigned char cast — this is one of the most commonly violated C rules. Writing
islower(c)where c is a signed char is undefined behavior for characters above 127. Always cast:islower((unsigned char)c). - toupper/tolower are safer than ASCII arithmetic — some beginners convert case by adding or subtracting 32 (the ASCII difference between ‘A’ and ‘a’). This only works for ASCII and silently corrupts non-letter characters.
toupper()/tolower()are locale-aware and check the character category first. - In-place string modification — the loop modifies the string in place by writing back to
sentence[i]. No second buffer is needed. This is efficient and idiomatic for character-by-character transformations.
Related Programs
- ctype.h Character Classification in C
- Substring Search with strstr() in C
- String Functions in C — Complete Guide
- atof, atoi, and fgets in C
- Pointers in C — Complete Guide
Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
|
C Programming: A Modern Approach — K.N. King (India) |
(US)
Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.