0
0
CProgramBeginner · 2 min read

C Program to Convert Lowercase to Uppercase

You can convert lowercase to uppercase in C by checking if a character is between 'a' and 'z' and then subtracting 32 from its ASCII value, like if(c >= 'a' && c <= 'z') c = c - 32;.
📋

Examples

Inputhello
OutputHELLO
Inputworld123
OutputWORLD123
InputaBcDeF
OutputABCDEF
🧠

How to Think About It

To convert lowercase letters to uppercase, check each character to see if it is a lowercase letter by comparing it with 'a' and 'z'. If it is, convert it by subtracting 32 from its ASCII code because uppercase letters are 32 positions before lowercase letters in ASCII. Leave other characters unchanged.
📐

Algorithm

1
Get the input string from the user.
2
For each character in the string, check if it is between 'a' and 'z'.
3
If yes, subtract 32 from its ASCII value to convert it to uppercase.
4
If not, leave the character as it is.
5
Print the converted string.
💻

Code

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] >= 'a' && str[i] <= 'z') {
            str[i] = str[i] - 32;
        }
    }

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

Dry Run

Let's trace the input 'hello' through the code

1

Input string

str = 'hello\0'

2

Check first character 'h'

'h' is between 'a' and 'z', so convert: 'h' (ASCII 104) - 32 = 'H' (ASCII 72)

3

Check second character 'e'

'e' is lowercase, convert to 'E'

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'

IndexOriginal CharIs Lowercase?Converted Char
0hYesH
1eYesE
2lYesL
3lYesL
4oYesO
💡

Why This Works

Step 1: Check if character is lowercase

The code uses if (str[i] >= 'a' && str[i] <= 'z') to find lowercase letters because ASCII codes for 'a' to 'z' are continuous.

Step 2: Convert by subtracting 32

Subtracting 32 from the ASCII value converts a lowercase letter to uppercase since uppercase letters are 32 positions before lowercase in ASCII.

Step 3: Leave other characters unchanged

Characters outside 'a' to 'z' range remain the same, so numbers and symbols are not affected.

🔄

Alternative Approaches

Using built-in function toupper()
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++) {
        str[i] = toupper(str[i]);
    }

    printf("Uppercase string: %s", str);
    return 0;
}
This method is simpler and safer because it handles locale and non-alphabetic characters properly.
Manual ASCII check with function
c
#include <stdio.h>

char to_upper(char c) {
    if (c >= 'a' && c <= 'z')
        return c - 32;
    else
        return c;
}

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

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

    printf("Uppercase string: %s", str);
    return 0;
}
This approach separates logic into a function, improving readability and reusability.

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

Time Complexity

The program loops through each character once, so time grows linearly with input size, making it O(n).

Space Complexity

The conversion is done in-place without extra arrays, so space complexity is O(1).

Which Approach is Fastest?

Manual ASCII subtraction and toupper() both run in O(n), but toupper() is optimized and safer for different locales.

ApproachTimeSpaceBest For
Manual ASCII subtractionO(n)O(1)Simple ASCII-only conversion
Using toupper()O(n)O(1)Locale-aware and safer conversion
Function-based manual checkO(n)O(1)Readable and reusable code
💡
Use the built-in toupper() function for cleaner and locale-aware uppercase conversion.
⚠️
Forgetting to check if the character is lowercase before subtracting 32 can corrupt non-letter characters.