C Program to read an English sentence and replace lowercase characters by uppercase and vice-versa.

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) and islower((unsigned char)c) — the ctype.h functions take an int argument that must be in the range [0, 255] or equal to EOF. On platforms where char is signed, characters with values 128–255 become negative integers, which are out of range and trigger undefined behavior. Casting to unsigned char before passing to ctype.h functions is the correct and portable pattern.
  • toupper() and tolower() — return the converted character as an int. Cast the result back to char before 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 inputfgets(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

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>