C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Strings in C

Convert string to uppercase

C Program: Convert String to Uppercase (without using strupr())

C

#include <stdio.h>

 

int main() {

    char str[200];

    int i;

 

    // Input string safely

    printf("Enter a string: ");

    fgets(str, sizeof(str), stdin);

 

    // Convert to uppercase

    for (i = 0; str[i] != '\0'; i++) {

        if (str[i] >= 'a' && str[i] <= 'z') {

            str[i] = str[i] - 32;  // Convert lowercase to uppercase (ASCII difference)

        }

    }

 

    printf("Uppercase string: %s", str);

 

    return 0;

}

Output

 
OUTPUT :
Enter a string: It Developer
Uppercase string: IT DEVELOPER

Explanation

  • The program takes a string as input using fgets() to include spaces.
  • Each character is checked:
    • If it’s between 'a' and 'z', it’s converted to uppercase by subtracting 32 (the ASCII value difference between lowercase and uppercase letters).
  • The converted string is then displayed.