Q:

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

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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!!!

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Most popular and Searched C++ solved programs with Explanation and Output

Similar questions


need a help?


find thousands of online teachers now
C++ program to sort an array in Ascending Order... >>
<< C++ program to check leap year...