0
0
CHow-ToBeginner · 2 min read

C Program to Convert String to Uppercase

In C, convert a string to uppercase by looping through each character and using toupper() from ctype.h, like str[i] = toupper(str[i]); inside a loop.
📋

Examples

Inputhello
OutputHELLO
InputC Programming
OutputC PROGRAMMING
Input123abc!
Output123ABC!
🧠

How to Think About It

To convert a string to uppercase, look at each character one by one. If the character is a lowercase letter, change it to its uppercase form. Keep other characters unchanged. Use the toupper() function to do this easily.
📐

Algorithm

1
Get the input string.
2
For each character in the string:
3
Check if it is a lowercase letter.
4
If yes, convert it to uppercase using <code>toupper()</code>.
5
Move to the next character until the end of the string.
6
Return or print the modified string.
💻

Code

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

int main() {
    char str[100];
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    for (int i = 0; str[i] != '\0'; i++) {
        if (str[i] == '\n') {
            str[i] = '\0';
            break;
        }
        str[i] = toupper(str[i]);
    }

    printf("Uppercase string: %s", str);
    return 0;
}
Output
Enter a string: Hello World Uppercase string: HELLO WORLD
🔍

Dry Run

Let's trace the string "abc" through the code

1

Start with input string

str = "abc"

2

Convert first character

str[0] = toupper('a') -> 'A'

3

Convert second character

str[1] = toupper('b') -> 'B'

4

Convert third character

str[2] = toupper('c') -> 'C'

IndexOriginal CharUppercase Char
0aA
1bB
2cC
💡

Why This Works

Step 1: Use toupper() function

The toupper() function converts a lowercase letter to uppercase if possible; otherwise, it returns the character unchanged.

Step 2: Loop through the string

We check each character until we reach the string's end marked by \0.

Step 3: Modify characters in place

We replace each character in the original string with its uppercase version directly.

🔄

Alternative Approaches

Manual ASCII conversion
c
#include <stdio.h>

int main() {
    char str[100];
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    for (int i = 0; str[i] != '\0'; i++) {
        if (str[i] == '\n') {
            str[i] = '\0';
            break;
        }
        if (str[i] >= 'a' && str[i] <= 'z') {
            str[i] = str[i] - ('a' - 'A');
        }
    }

    printf("Uppercase string: %s", str);
    return 0;
}
This method uses ASCII values to convert letters without <code>ctype.h</code>, but is less readable.
Using a separate output string
c
#include <stdio.h>
#include <ctype.h>

int main() {
    char str[100], upper[100];
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    for (int i = 0; str[i] != '\0'; i++) {
        if (str[i] == '\n') {
            upper[i] = '\0';
            break;
        }
        upper[i] = toupper(str[i]);
    }

    printf("Uppercase string: %s\n", upper);
    return 0;
}
This keeps the original string unchanged and stores uppercase in a new string, using more memory.

Complexity: O(n) time, O(1) space

Time Complexity

The code loops through each character once, so time grows linearly with string length.

Space Complexity

The conversion happens in place, so no extra space is needed besides the input string.

Which Approach is Fastest?

Using toupper() is efficient and readable; manual ASCII math is similar in speed but less clear.

ApproachTimeSpaceBest For
toupper() in placeO(n)O(1)Simple and readable code
Manual ASCII conversionO(n)O(1)No library dependency
Separate output stringO(n)O(n)Preserving original string
💡
Include ctype.h and use toupper() for easy and safe uppercase conversion.
⚠️
Forgetting to include ctype.h or not checking for the string's end causes errors.