0
0
CProgramBeginner · 2 min read

C Program to Convert String to Uppercase

Use a loop to check each character of the string and convert it to uppercase by subtracting 32 if it is a lowercase letter, like if(str[i] >= 'a' && str[i] <= 'z') str[i] = str[i] - 32;.
📋

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 (between 'a' and 'z'), change it to uppercase by moving it to the corresponding uppercase letter (between 'A' and 'Z'). Leave other characters unchanged.
📐

Algorithm

1
Get the input string from the user.
2
Start from the first character and go through each character until the end of the string.
3
For each character, check if it is a lowercase letter between 'a' and 'z'.
4
If yes, convert it to uppercase by subtracting 32 from its ASCII value.
5
Move to the next character and repeat until the string ends.
6
Print the converted uppercase string.
💻

Code

c
#include <stdio.h>

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

    while (str[i] != '\0') {
        if (str[i] >= 'a' && str[i] <= 'z') {
            str[i] = str[i] - 32;
        }
        i++;
    }

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

Dry Run

Let's trace the input "Hello" through the code

1

Input string

str = "Hello\0"

2

Check first character 'H'

'H' is not between 'a' and 'z', so no change

3

Check second character 'e'

'e' is lowercase, convert to 'E' by subtracting 32

4

Check third character 'l'

'l' is lowercase, convert to 'L'

5

Check fourth character 'l'

'l' is lowercase, convert to 'L'

6

Check fifth character 'o'

'o' is lowercase, convert to 'O'

7

End of string

Stop loop at '\0'

IndexCharacter BeforeIs Lowercase?Character After
0HNoH
1eYesE
2lYesL
3lYesL
4oYesO
💡

Why This Works

Step 1: Check each character

The program looks at each character in the string one by one using a loop.

Step 2: Identify lowercase letters

It uses if(str[i] >= 'a' && str[i] <= 'z') to find lowercase letters.

Step 3: Convert to uppercase

It converts lowercase letters to uppercase by subtracting 32 from their ASCII value, because uppercase letters are 32 positions before lowercase in ASCII.

🔄

Alternative Approaches

Using built-in function toupper()
c
#include <stdio.h>
#include <ctype.h>

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

    while (str[i] != '\0') {
        str[i] = toupper(str[i]);
        i++;
    }

    printf("Uppercase string: %s", str);
    return 0;
}
This uses the standard library function <code>toupper()</code> which handles conversion safely and clearly.
Using ASCII difference constant
c
#include <stdio.h>

#define DIFF ('a' - 'A')

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

    while (str[i]) {
        if (str[i] >= 'a' && str[i] <= 'z') {
            str[i] = str[i] - DIFF;
        }
        i++;
    }

    printf("Uppercase string: %s", str);
    return 0;
}
Defines the difference between lowercase and uppercase once, improving readability.

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

Time Complexity

The program 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 usually optimized and clearer, but manual ASCII subtraction is also fast and simple.

ApproachTimeSpaceBest For
Manual ASCII check and subtractO(n)O(1)Simple programs without extra includes
Using toupper() functionO(n)O(1)Cleaner code and handling locale
Using ASCII difference constantO(n)O(1)Readable manual conversion
💡
Use the standard toupper() function for cleaner and safer uppercase conversion.
⚠️
Forgetting to check if the character is lowercase before converting can change non-letter characters incorrectly.