C Program to Convert an Uppercase String to a Lowercase String

In this article, we will write a C program to convert an uppercase string to a lowercase string. The program takes a string from the user as input, converts it to lowercase and prints it on the output window.

Sample Examples:

Input:  "HELLO WORLD!"
Output:  "hello world!'

Input:  "This Is A StrinG "
Output:  "this is a string'

There are actually two ways to convert a string from uppercase to lowercase. First, use the built-in function strlwr() provided by the <string.h> library, and second, write your own custom logic and manipulate the ASCII code.

Before we dive into the implementation of the actual program, we have to first understand how the characters are stored in the memory of your computer.

In most computer systems, characters are stored using a character encoding called ASCII. ASCII assigns a unique numerical value (ASCII code) to each character, including letters, digits, and special symbols.

So, when you type the character ‘A’ on your computer, the computer actually stores its ASCII value 65 instead of storing the character ‘A’ itself.

The ASCII code for uppercase letters A, B, C…Z ranges from 65 to 90 whereas the ASCII code for lowercase letters a, b, c….z ranges from 97 to 122.

So, if you want to convert an uppercase letter to a lowercase letter, you can simply add 32 to its ASCII value. For example, the ASCII value of ‘A’ is 65, so if you add 32 to it, it will become 97 which is the ASCII value of ‘a’.

Here is the formula:

lowercase = uppercase + 32

So, if you have a string in uppercase letters and you want to convert it to lowercase, you can loop through it and convert each character to lowercase one by one.

See implementation in the following program:

// C program to convert uppercase string to lowercase

#include <stdio.h>
#include <conio.h>
#include <string.h>

int main() {

    char str[100];

    printf("Enter a string: ");
	gets(str);

	// Loop through the string
    for(int i = 0; i < strlen(str); i++) {
    	
    	//  Check if current character is uppercase
        if (str[i] >= 65 && str[i] <= 90) {
        	
        	// Convert to lowercase
            str[i] = str[i] + 32;
        }
    }

    printf("String after conversion = %s", str);

    return 0;

}

Output:

Enter a string: THIS IS A STRING
String after conversion = this is a string

In this program, we are looping through the string entered by the user and checking if the current character is uppercase. If yes, we add 32 to it, so that it becomes lowercase.

If the current character is lowercase, we don’t do anything.


2. Using a While Loop to Convert the String to Lowercase

In C, a string is always terminated by a null character('\0'). So, when you are using a while loop to iterate through a string, you can check if the current character is a null character('\0'). If yes, terminate the loop.

The base logic remains as it is in the case of the for loop.

See this example:

// C program to convert uppercase string to lowercase

#include <stdio.h>
#include <conio.h>
#include <string.h>

int main() {

    char str[100];
    int i = 0;

    printf("Enter a string: ");
	gets(str);

	// Loop through the string
    while(str[i] != '\0'){
    	
    	//  Check if current character is uppercase
        if (str[i] >= 65 && str[i] <= 90) {
        	
        	// Convert to lowercase
            str[i] = str[i] + 32;
        }
        
        // Increment on each iteration
        i++;
    }

    printf("String after conversion = %s", str);

    return 0;

}

Output:

Enter a string: HELLO wORLD!!
String after conversion = hello world!!

3. Using the inbuilt Function strlwr() to Convert String to Lowercase

If you don’t want to build your own custom logic to convert a string from uppercase to lowercase, you can use the built-in function strlwr() provided by the <string.h> header library.

The strlwr() function takes a single argument, which is the string that you want to convert to lowercase. It performs the conversion on the original string itself.

See implementation in the following program:

// C program to convert uppercase string to lowercase
// using the strlwr() function

#include <stdio.h>
#include <string.h>

int main() {

    char str[100];
    
    printf("Enter a string: ");
	gets(str);

	// Convert string to lowercase
	strlwr(str);
	
    printf("String after conversion = %s", str);

    return 0;

}

Output:

Enter a string: THIS IS A RANDOM STRING 123
String after conversion = this is a random string 123

As you can see, the conversion happens in just a single line. So, this method can be very useful when you are working on a large codebase.

I hope you will find this article helpful. Thanks for reading!

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

Leave a Comment