0
0
CHow-ToBeginner · 2 min read

C Program to Convert String to Lowercase

In C, you can convert a string to lowercase by looping through each character and using the tolower() function from ctype.h, like this: str[i] = tolower(str[i]); inside a loop.
📋

Examples

InputHELLO
Outputhello
InputHeLLo WoRLd!
Outputhello world!
Input1234!@#
Output1234!@#
🧠

How to Think About It

To convert a string to lowercase, think of checking each character one by one. If the character is an uppercase letter, change it to its lowercase form. Otherwise, leave it as is. This way, the whole string becomes lowercase.
📐

Algorithm

1
Get the input string.
2
For each character in the string:
3
Check if it is uppercase.
4
If yes, convert it to lowercase.
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[] = "HeLLo WoRLd!";
    for (int i = 0; str[i] != '\0'; i++) {
        str[i] = tolower((unsigned char)str[i]);
    }
    printf("%s\n", str);
    return 0;
}
Output
hello world!
🔍

Dry Run

Let's trace the string "HeLLo WoRLd!" through the code

1

Start with first character

Character 'H' is uppercase, convert to 'h'

2

Next characters

Characters 'e', 'L', 'L', 'o' converted to 'e', 'l', 'l', 'o' respectively

3

Continue to end

Spaces and punctuation remain unchanged; uppercase letters converted

IndexOriginal CharConverted Char
0Hh
1ee
2Ll
3Ll
4oo
5
6Ww
7oo
8Rr
9Ll
10dd
11!!
💡

Why This Works

Step 1: Using tolower()

The tolower() function converts uppercase letters to lowercase and leaves other characters unchanged.

Step 2: Looping through string

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

Step 3: In-place modification

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

🔄

Alternative Approaches

Manual ASCII conversion
c
#include <stdio.h>

int main() {
    char str[] = "HeLLo WoRLd!";
    for (int i = 0; str[i] != '\0'; i++) {
        if (str[i] >= 'A' && str[i] <= 'Z') {
            str[i] = str[i] + ('a' - 'A');
        }
    }
    printf("%s\n", str);
    return 0;
}
This method manually converts uppercase letters by adding the ASCII difference; it avoids using <code>ctype.h</code> but is less readable.
Create new lowercase string
c
#include <stdio.h>
#include <ctype.h>

int main() {
    char str[] = "HeLLo WoRLd!";
    char lower[100];
    int i = 0;
    while (str[i] != '\0') {
        lower[i] = tolower((unsigned char)str[i]);
        i++;
    }
    lower[i] = '\0';
    printf("%s\n", lower);
    return 0;
}
This method creates a new string for lowercase letters, preserving the original string but uses extra 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 is done in-place, so no extra space is needed beyond the input string.

Which Approach is Fastest?

Using tolower() is efficient and readable; manual ASCII conversion is similar in speed but less clear; creating a new string uses more memory.

ApproachTimeSpaceBest For
Using tolower()O(n)O(1)Simple and safe in-place conversion
Manual ASCII conversionO(n)O(1)Avoids library but less readable
New lowercase stringO(n)O(n)Preserving original string, uses extra memory
💡
Include ctype.h and use tolower() for safe and simple lowercase conversion.
⚠️
Forgetting to check for the string's null terminator \0 causes infinite loops or crashes.