C program to Convert String to Lowercase

How to write a C Program to Convert String to Lowercase without using 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 string or character array. Next, it will use For Loop to iterate each character in that string, and convert them to Lowercase.

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

#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. Below statements asks the user to enter any word, and assign the user entered a string to Str variable. Please refer to strlwr function as well.

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

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

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

User Entered the String “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 lowercase e

Do the same for remaining iterations

C program to Convert String to Lowercase using For Loop

This program to convert string to lowercase 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.

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

#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;
}

C string lowercase output

 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 lowercase is the same as the above. But this time we are using While Loop (replacing C Programming For Loop with While Loop).

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

#include <stdio.h> 

int main()
{
	char str[100];
	int i;
 
	printf("\n Please Enter a String to convert it into Lowercase :  ");
	gets(str);
 
 	i = 0;
	while (str[i] != '\0') 
	{
    	if (str[i] >= 'A' && str[i] <= 'Z') 
		{
        	str[i] = str[i] + 32;
    	}
      	i++;
	}
 
	printf("\n The given String in Lower Case = %s", str);
 
	return 0;
}
 Please Enter a String to convert it into Lowercase : C PROGRAMMING

 The given String in Lower Case = c programming

Program to Convert String to Lowercase Using Functions

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

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

#include <stdio.h> 

void String_Lower(char []);
 
int main()
{
	char str[100];
 
	printf("\n Please Enter a string to convert it into Lowercase : ");
	gets(str);
 
	String_Lower(str);
 
	printf("\n The given String in Lower Case = %s", str);
 
	return 0;
}
 
void String_Lower(char string[]) 
{
	int i = 0;
 
	while (string[i] != '\0') 
	{
    	if (string[i] >= 'A' && string[i] <= 'Z') 
		{
        	string[i] = string[i] + 32;
    	}
      	i++;
	}
}

C string lowercase output

 Please Enter a string to convert it into Lowercase : LEARN C PROGRAMMING

 The given String in Lower Case = learn c programming