C++ Program to Toggle Character Cases in a String

Write a C++ Program to Toggle Character Cases in a String with an example. In this C++ toggle string character cases example, we used the Else If statement within the for loop (for (int i = 0; i < lwupTxt.length(); i++)). The first if statement uses an islower function (if(islower(lwupTxt[i]))) to look for lowercase characters. Then, it converts them to uppercase using the toupper. The else if statement uses an isupper (else if(isupper(lwupTxt[i]))) to check for uppercase characters. Then, it uses the tolower function to convert them to lowercase.

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

int main()
{
	string lwupTxt;
	
	cout << "\nPlease Enter the String to Toggle Case  =  ";
	getline(cin, lwupTxt);
	
	for (int i = 0; i < lwupTxt.length(); i++)
  	{
  		if(islower(lwupTxt[i]))
  		{
  			lwupTxt[i] = toupper(lwupTxt[i]);
  		}
  		else if(isupper(lwupTxt[i]))
  		{
  			lwupTxt[i] = tolower(lwupTxt[i]);
		}
  	}
  	
	cout<< "\nThe Given String After Toggle Case = " << lwupTxt;
		
 	return 0;
}
C++ Program to Toggle Character Cases in a String 1

In this C++ code to toggle string characters, instead of using the built-in islower and isupper functions, we used the a, z, A, Z to check whether the character is lowercase or uppercase. If it is lowercase, we are adding 32 to the ASCII value, and if it is uppercase, we are subtracting 32 from it.

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

int main()
{
	string lwupTxt;
	
	cout << "\nPlease Enter the String to Toggle Case  =  ";
	getline(cin, lwupTxt);
	
	for (int i = 0; i < lwupTxt.length(); i++)
  	{
  		if(lwupTxt[i] >= 'a' && lwupTxt[i] <= 'z')
  		{
  			lwupTxt[i] = lwupTxt[i] - 32;
  		}
  		else if(lwupTxt[i] >= 'A' && lwupTxt[i] <= 'Z')
  		{
  			lwupTxt[i] = lwupTxt[i] + 32;
		}
  	}
  	
	cout<< "\nThe Given String After Toggle Case = " << lwupTxt;
		
 	return 0;
}
Please Enter the String to Toggle Case  =  Tutorial GATEwaY

The Given String After Toggle Case = tUTORIAL gateWAy

C++ Program to Toggle Character Cases in a String using ASCII values

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

int main()
{
	string lwupTxt;
	
	cout << "\nPlease Enter the String to Toggle Case  =  ";
	getline(cin, lwupTxt);
	
	for (int i = 0; i < lwupTxt.length(); i++)
  	{
  		if(lwupTxt[i] >= 65 && lwupTxt[i] <= 90)
  		{
  			lwupTxt[i] = lwupTxt[i] + 32;
  		}
  		else if(lwupTxt[i] >= 97 && lwupTxt[i] <= 122)
  		{
  			lwupTxt[i] = lwupTxt[i] - 32;
		}
  	}
  	
	cout<< "\nThe Given String After Toggle Case = " << lwupTxt;
		
 	return 0;
}
Please Enter the String to Toggle Case  =  HellO WoRLd!

The Given String After Toggle Case = hELLo wOrlD!

C++ example to Toggle Character Cases in a String using a while loop

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

int main()
{
	string lwupTxt;
	int i = 0; 
	
	cout << "\nPlease Enter the String to Toggle Case  =  ";
	getline(cin, lwupTxt);
	
	while(i < lwupTxt.length())
  	{
  		if(lwupTxt[i] >= 'a' && lwupTxt[i] <= 'z')
  		{
  			lwupTxt[i] = lwupTxt[i] - 32;
  		}
  		else if(lwupTxt[i] >= 'A' && lwupTxt[i] <= 'Z')
  		{
  			lwupTxt[i] = lwupTxt[i] + 32;
		}
		i++;
  	}
  	
	cout<< "\nThe Given String After Toggle Case = " << lwupTxt;
		
 	return 0;
}
Please Enter the String to Toggle Case  =  C++ ProGRAMMinG

The Given String After Toggle Case = c++ pROgrammINg

C++ Toggle String Character Cases using Functions

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

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

int main()
{
	string lwupTxt;
	
	cout << "\nPlease Enter the String to Toggle Case  =  ";
	getline(cin, lwupTxt);
	
	string lwup = stringLower(lwupTxt);
  	
	cout<< "\nThe Given String After Toggle Case = " << lwup;
		
 	return 0;
}
Please Enter the String to Toggle Case  =  LearN C++ ProGRaMS

The Given String After Toggle Case = lEARn c++ pROgrAms