0
0
CProgramBeginner · 2 min read

C Program to Convert Uppercase to Lowercase

Use a C program that reads a character and converts it to lowercase by adding 32 if it is uppercase, like if(c >= 'A' && c <= 'Z') c = c + 32;.
📋

Examples

InputA
Outputa
InputHELLO
Outputhello
InputZ
Outputz
🧠

How to Think About It

To convert uppercase letters to lowercase, check if a character is between 'A' and 'Z'. If yes, add 32 to its ASCII value to get the lowercase letter. This works because uppercase and lowercase letters differ by 32 in ASCII.
📐

Algorithm

1
Get input string from the user
2
For each character in the string, check if it is uppercase (between 'A' and 'Z')
3
If uppercase, convert it to lowercase by adding 32
4
Print the converted 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("Lowercase string: %s", str);
    return 0;
}
Output
Enter a string: HELLO Lowercase string: hello
🔍

Dry Run

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

1

Input string

str = 'HELLO\n'

2

Check first character 'H'

'H' is between 'A' and 'Z', so convert: 'H' + 32 = 'h'

3

Check second character 'E'

'E' is uppercase, convert to 'e'

4

Check third character 'L'

'L' is uppercase, convert to 'l'

5

Check fourth character 'L'

'L' is uppercase, convert to 'l'

6

Check fifth character 'O'

'O' is uppercase, convert to 'o'

7

End of string

Stop at '\0' character

IndexOriginal CharIs Uppercase?Converted Char
0HYesh
1EYese
2LYesl
3LYesl
4OYeso
💡

Why This Works

Step 1: Check uppercase range

The code uses if(c >= 'A' && c <= 'Z') to find uppercase letters because uppercase letters are continuous in ASCII.

Step 2: Convert by adding 32

Adding 32 to the ASCII value converts uppercase letters to lowercase since 'a' is 32 positions after 'A'.

Step 3: Process each character

The program loops through each character to convert all uppercase letters in the string.

🔄

Alternative Approaches

Using built-in function tolower()
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] = tolower(str[i]);
        i++;
    }
    printf("Lowercase string: %s", str);
    return 0;
}
This method is simpler and safer, using the standard library function tolower() which handles locale and non-alphabetic characters.
Using bitwise OR operation
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("Lowercase string: %s", str);
    return 0;
}
Using bitwise OR with 32 converts uppercase to lowercase by setting the 6th bit, which is a fast trick but less readable.

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

Time Complexity

The program loops through each character once, so time complexity is O(n) where n is the string length.

Space Complexity

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

Which Approach is Fastest?

Using bitwise OR is fastest due to low-level operation, but using tolower() is safer and more readable.

ApproachTimeSpaceBest For
Manual ASCII check and add 32O(n)O(1)Simple, clear logic
Using tolower() functionO(n)O(1)Safety and locale support
Bitwise OR with 32O(n)O(1)Performance and low-level optimization
💡
Remember uppercase and lowercase letters differ by 32 in ASCII, so adding 32 converts uppercase to lowercase.
⚠️
A common mistake is not checking if the character is uppercase before adding 32, which can corrupt non-uppercase characters.