C program to Convert String to Lowercase

How to write a C Program to Convert String to Lowercase without using the strlwr function. We can convert the string to lowercase in multiple ways, but we will discuss four different approaches: using For Loop, While Loop, Functions, and ASCII Values.

C program to Convert String to Lowercase without using strlwr()

This program allows the user to enter any character array. Next, it will use For Loop to iterate each character in that string and convert them to Lowercase.

#include <stdio.h>
#include <string.h>
 
int main()
{
  	char Str1[100];
  	int i;
 
  	printf("\n Please Enter a String to Convert into Lowercase :  ");
  	gets(Str1);
  	
  	for (i = 0; Str1[i]!='\0'; i++)
  	{
  		if(Str1[i] >= 'A' && Str1[i] <= 'Z')
  		{
  			Str1[i] = Str1[i] + 32;
		}
  	}

  	printf("\n The given String in Lower Case = %s", Str1);
  	
  	return 0;
}
C program to Convert String to Lowercase without using strlwr 1

We declared a character array of size 100. The below statements ask the user to enter any word and assign the user-entered char array to the Str variable. Please refer to the strlwr function as well.

printf("\n Please Enter any String :  ");
gets(Str);

In the next line of the C Program, we have For Loop to iterate and convert the string to lowercase,

for (i = 0; Str1[i]!='\0'; i++)
{
	if(Str1[i] >= 'A' && Str1[i] <= 'Z')
	{
		Str1[i] = Str1[i] + 32;
	}
}

User Entered text = “hELLo”

First Iteration: for (i = 0; Str[0]!=’\0′; 0++)
(Str[0] != ‘\0’) condition is TRUE because Str[0] = h
if(Str1[i] >= ‘A’ && Str1[i] <= ‘Z’) – Condition is False

Second Iteration: for (i = 1; Str[1]!=’\0′; 1++)
(Str[1] != ‘\0’) condition is TRUE because Str[1] = E
if(Str1[i] >= ‘A’ && Str1[i] <= ‘Z’) – Condition is True
Str1[i] = Str1[i] + 32 => 69 + 32
Str1[i] = 101
101 is the ASCII code for e

Do the same for the remaining iterations

C program to Convert String to Lowercase using For Loop and ASCII

This program is the same as above. However, this time we are using ASCII values inside the If Statement. I suggest you refer to the ASCII Table article.

#include <stdio.h>
#include <string.h>
 
int main()
{
  	char Str1[100];
  	int i;
 
  	printf("\n Please Enter a String that you want to Convert into Lowercase :  ");
  	gets(Str1);
  	
  	for (i = 0; Str1[i]!='\0'; i++)
  	{
  		if(Str1[i] >= 65 && Str1[i] <= 90)
  		{
  			Str1[i] = Str1[i] + 32;
		}
  	}

  	printf("\n The Given String in Lower Case = %s", Str1);
  	
  	return 0;
}
 Please Enter a String that you want to Convert into Lowercase :  TUTORIAL GATEWAY

 The Given String in Lower Case = tutorial gateway

C program to Convert String to Lowercase Using While Loop

This program to convert string to lower case is the same as the above. But this time, we are using While Loop (replacing C Programming For Loop with While Loop).

#include <stdio.h> 

int main()
{
	char str[100];
	int i;
 
	printf("\n Please Enter any upper text :  ");
	gets(str);
 
 	i = 0;
	while (str[i] != '\0') 
	{
    	if (str[i] >= 'A' && str[i] <= 'Z') 
		{
        	str[i] = str[i] + 32;
    	}
      	i++;
	}
 
	printf("\n Lower Case = %s", str);
 
	return 0;
}
 Please Enter any upper text : C PROGRAMMING

 Lower Case = c programming

This C program to convert string to lowercase uses the Functions concept to separate the logic from the main.

#include <stdio.h> 

void StrLow(char []);
 
int main()
{
	char str[100];
 
	printf("\n Please Enter any text : ");
	gets(str);
 
	StrLow(str);
 
	printf("\n Lower Case = %s", str);
 
	return 0;
}
 
void StrLow(char str[]) 
{
	int i = 0;
 
	while (str[i] != '\0') 
	{
    	if (str[i] >= 'A' && str[i] <= 'Z') 
		{
        	str[i] = string[i] + 32;
    	}
      	i++;
	}
}
 Please Enter any text : LEARN C PROGRAMMING

 Lower Case = learn c programming