C program to convert upper case to lower and vice versa by using string concepts

Converting uppercase letters to lowercase and lowercase letters to uppercase is commonly known as case toggling. In C programming, we can achieve this using ASCII values or built-in character functions.

Syntax

// Manual approach using ASCII values
if (ch >= 'a' && ch = 'A' && ch 

Method 1: Using ASCII Values

This approach manually checks ASCII ranges and applies the conversion by adding or subtracting 32 −

#include <stdio.h>
#include <string.h>
#define MAX 100

void toggleCase(char *string);

int main() {
    char string[MAX];
    printf("Enter the string to toggle: ");
    fgets(string, MAX, stdin);
    
    // Remove newline character if present
    string[strcspn(string, "
")] = '\0'; printf("Original string: %s
", string); toggleCase(string); printf("Toggled string: %s
", string); return 0; } void toggleCase(char *string) { int i = 0; while (string[i] != '\0') { if (string[i] >= 'a' && string[i] <= 'z') { string[i] = string[i] - 32; // Convert to uppercase } else if (string[i] >= 'A' && string[i] <= 'Z') { string[i] = string[i] + 32; // Convert to lowercase } i++; } }
Enter the string to toggle: TutoRialS PoinT
Original string: TutoRialS PoinT
Toggled string: tUTOrIALs pOINt

Method 2: Using Built-in Functions

This method uses the standard library functions isupper(), islower(), toupper(), and tolower()

#include <stdio.h>
#include <ctype.h>

int main() {
    char string[] = "TutORial PoiNT";
    int i;
    
    printf("Original string: %s
", string); for (i = 0; string[i] != '\0'; i++) { if (isupper(string[i])) { string[i] = tolower(string[i]); } else if (islower(string[i])) { string[i] = toupper(string[i]); } } printf("Toggled string: %s
", string); return 0; }
Original string: TutORial PoiNT
Toggled string: tUTorIAL pOInT

Key Points

  • Method 1 uses ASCII values: lowercase (97-122), uppercase (65-90), difference is 32
  • Method 2 is more readable and handles locale-specific characters better
  • Both methods preserve non-alphabetic characters unchanged
  • Use fgets() instead of gets() for safe input handling

Conclusion

Case toggling can be implemented using ASCII arithmetic or standard library functions. The built-in function approach is preferred for better code readability and portability across different character encodings.

Updated on: 2026-03-15T13:19:03+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements