Home »
C++ programs »
C++ Most popular & searched programs
C++ program to convert lowercase character to uppercase and vice versa
Given a lowercase/uppercase character, we have to convert it into uppercase/lowercase using C++ program.
[Last updated : February 27, 2023]
Problem statement
In this program, we will learn how to convert an entered character from lowercase to uppercase and uppercase to lowercase in C++?
Converting lowercase character to uppercase and vice versa
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.
C++ program to convert lowercase character to uppercase and vice versa
#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!!!