C++ Program to Convert String to Uppercase

Write a C++ Program to Convert String to Uppercase with an example. In this C++ program, we used for loop (for (int i = 0; i < lwTxt.length(); i++)) to traverse lwTxt string characters. Within that, we used the toupper function (lwTxt[i] = toupper(lwTxt[i])) to convert lowercase string to uppercase.

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

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

 The C++ algorithm file has a transform function that converts the lowercase string to uppercase.

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

int main()
{
	string lwTxt;
	
	cout << "\nPlease Enter the String to Convert into Uppercase  =  ";
	getline(cin, lwTxt);
	
	std:: transform(lwTxt.begin(), lwTxt.end(), lwTxt.begin(), ::toupper);
	
	cout<< "\nThe Given String in Uppercase = " << lwTxt;
		
 	return 0;
}
Please Enter the String to Convert into Uppercase  =  hello world

The Given String in Uppercase = HELLO WORLD

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

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

int main()
{
	string lwTxt;
	int i = 0; 
	
	cout << "\nPlease Enter the String to Convert into Uppercase  =  ";
	getline(cin, lwTxt);
	
	while(i < lwTxt.length())
  	{
  		if(islower(lwTxt[i]))
  		{
  			lwTxt[i] = toupper(lwTxt[i]);
		}
		i++;
  	}
  	
	cout<< "\nThe Given String in Uppercase = " << lwTxt;
		
 	return 0;
}
Please Enter the String to Convert into Uppercase  =  c++ Programs

The Given String in Uppercase = C++ PROGRAMS

In this C++ code to Convert String to Uppercase, instead of converting all the characters, we used the if statement (if(lwTxt[i] >= ‘a’ && lwTxt[i] <= ‘z’)) to check whether the character is lowercase. If true, we are subtracting 32 from the ASCII Value to get the uppercase character.

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

int main()
{
	string lwTxt;
	
	cout << "\nPlease Enter the String to Convert into Uppercase  =  ";
	getline(cin, lwTxt);
	
	for (int i = 0; i < lwTxt.length(); i++)
  	{
  		if(lwTxt[i] >= 'a' && lwTxt[i] <= 'z')
  		{
  			lwTxt[i] = lwTxt[i] - 32;
		}
  	}
  	
	cout<< "\nThe Given String in Uppercase = " << lwTxt;
		
 	return 0;
}
Please Enter the String to Convert into Uppercase  =  tutorial gateway

The Given String in Uppercase = TUTORIAL GATEWAY

In this C++ Convert String to Uppercase example, we used the If statement. Within the If statement (if(lwTxt[i] >= 97 && lwTxt[i] <= 122 )), we used ASCII values to identify the lowercase characters in a string.

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

int main()
{
	string lwTxt;
	
	cout << "\nPlease Enter the String to Convert into Uppercase  =  ";
	getline(cin, lwTxt);
	
	for (int i = 0; i < lwTxt.length(); i++)
  	{
  		if(lwTxt[i] >= 97 && lwTxt[i] <= 122 )
  		{
  			lwTxt[i] = lwTxt[i] - 32;
		}
  	}
  	
	cout<< "\nThe Given String in Uppercase = " << lwTxt;
		
 	return 0;
}
Please Enter the String to Convert into Uppercase  =  tutorialgateway

The Given String in Uppercase = TUTORIALGATEWAY

C++ Program to Convert String to Uppercase using Functions

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

string stringUpper(string lwTxt)
{
	for (int i = 0; i < lwTxt.length(); i++)
  	{
  		if(lwTxt[i] >= 'a' && lwTxt[i] <= 'z')
  		{
  			lwTxt[i] = lwTxt[i] - 32;
		}
  	}
  	return lwTxt;
}
int main()
{
	string lwTxt;
	
	cout << "\nPlease Enter the String to Convert into Uppercase  =  ";
	getline(cin, lwTxt);
	
	string up = stringUpper(lwTxt);
  	
	cout<< "\nThe Given String in Uppercase = " << up;
		
 	return 0;
}
Please Enter the String to Convert into Uppercase  =  hello 2020 world!

The Given String in Uppercase = HELLO 2020 WORLD!

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.