Home » C++ programs » C++ Most popular & searched programs

C++ program to convert lowercase character to uppercase and vice versa

In this program, we will learn how to convert an entered character from lowercase to uppercase and uppercase to lowercase in C++?

Here, we will read a character and check whether it is a valid alphabet or not, if it is a valid alphabet (from A-Z or a-z), we will check uppercase character, if it is uppercase character - we will convert it into lowercase character and if it is in Lowercase character - we will convert it into uppercase character.

To convert uppercase to lowercase - we are adding 32 in the ASCII value of uppercase character because the difference between Uppercase and Lowercase characters are 32. Same to convert lowercase character to uppercase - we are subtracting 32 in the ASCII value of lowercase character.

Program to convert lowercase to uppercase and vice verse in C++



#include <iostream>
using namespace std;


int main()
{
	char ch;
	
	cout<<"Please input a valid character (Alphabet): ";
	cin>>ch;
	
	//check for valid alphabet
	if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
	{
		//check case and convert into opposite case
		if(ch>='A' && ch<='Z')
			ch=ch+32;
		else if(ch>='a' && ch<='z')
			ch=ch-32;
		else
			;	//none
		
		cout<<"converted character is: "<<ch<<endl;
				
	}
	else
	{
		cout<<"Entered character is not a valid alphabet!!!"<<endl;
	}
	
	return 0;
}

Output

First run:
Please input a valid character (Alphabet): H
converted character is: h

Second run:
Please input a valid character (Alphabet): h
converted character is: H

Third run:
Please input a valid character (Alphabet): &
Entered character is not a valid alphabet!!!





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.