C++ Program to Convert String to Lowercase

Write a C++ Program to Convert String to Lowercase with an example. In this C++ program, we used for loop to traverse upTxt string characters. We used the tolower function within the loop to convert uppercase string to lowercase.

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string upTxt;
	
	cout << "\nPlease Enter the String to Convert into Lowercase  =  ";
	getline(cin, upTxt);
	
	for (int i = 0; i < upTxt.length(); i++)
  	{
  		upTxt[i] = tolower(upTxt[i]);
  	}
  	
	cout<< "\nThe Given String in Lowercase = " << upTxt;
		
 	return 0;
}
Program to Convert String to Lowercase

The program has the std transform function under the algorithm file to convert uppercase string to lowercase.

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
	string upTxt;
	
	cout << "\nPlease Enter the String to Convert into Lowercase  =  ";
	getline(cin, upTxt);
	
	std:: transform(upTxt.begin(), upTxt.end(), upTxt.begin(), ::tolower);
  	
	cout<< "\nThe Given String in Lowercase = " << upTxt;
		
 	return 0;
}
Please Enter the String to Convert into Lowercase  =  HELLo WolLD!

The Given String in Lowercase = hello wolld!

C++ Example to Convert String to Lowercase using a While loop

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string upTxt;
	int i = 0; 
	
	cout << "\nPlease Enter the String to Convert into Lowercase  =  ";
	getline(cin, upTxt);
	
	while(i < upTxt.length())
  	{
  		if(isupper(upTxt[i]))
  		{
  			upTxt[i] = tolower(upTxt[i]);
		}
  		i++;
  	}
  	
	cout<< "\nThe Given String in Lowercase = " << upTxt;
		
 	return 0;
}
Please Enter the String to Convert into Lowercase  =  TutORIAL GATEWAY

The Given String in Lowercase = tutorial gateway

In this C++ program code to convert a string to Lowercase, we used an if statement to check whether the character is uppercase. If true, we are adding 32 to the ASCII Value to get the lowercase character.

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string upTxt;
	
	cout << "\nPlease Enter the String to Convert into Lowercase  =  ";
	getline(cin, upTxt);
	
	for (int i = 0; i < upTxt.length(); i++)
  	{
  		if(upTxt[i] >= 'A' && upTxt[i] <= 'Z')
  		{
  			upTxt[i] = upTxt[i] + 32;
		}
  	}
  	
	cout<< "\nThe Given String in Lowercase = " << upTxt;
		
 	return 0;
}
Please Enter the String to Convert into Lowercase  =  c++ PROGRAMS

The Given String in Lowercase = c++ programs

In this Convert String to Lowercase example, we used the If statement. Within the If statement, we used ASCII values to identify the uppercase characters in a string. Then, we are converting them to lowercase.

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string upTxt;
	
	cout << "\nPlease Enter the String to Convert into Lowercase  =  ";
	getline(cin, upTxt);
	
	for (int i = 0; i < upTxt.length(); i++)
  	{
  		if(upTxt[i] >= 65 && upTxt[i] <= 90)
  		{
  			upTxt[i] = upTxt[i] + 32;
		}
  	}
  	
	cout<< "\nThe Given String in Lowercase = " << upTxt;
		
 	return 0;
}
Please Enter the String to Convert into Lowercase  =  HI GUYS

The Given String in Lowercase = hi guys

C++ Program to Convert String to Lowercase using Functions

#include<iostream>
#include<string>
using namespace std;

string stringLower(string upTxt)
{
	for (int i = 0; i < upTxt.length(); i++)
  	{
  		if(upTxt[i] >= 'A' && upTxt[i] <= 'Z')
  		{
  			upTxt[i] = upTxt[i] + 32;
		}
  	}
  	return upTxt;
}

int main()
{
	string upTxt;
	
	cout << "\nPlease Enter the String to Convert into Lowercase  =  ";
	getline(cin, upTxt);
	
	string lw = stringLower(upTxt);
  	
	cout<< "\nThe Given String in Lowercase = " << lw;
		
 	return 0;
}
Please Enter the String to Convert into Lowercase  =  HI 2021 GUys

The Given String in Lowercase = hi 2021 guys

Also Read