PUBLISHED ON: APRIL 4, 2022
C++ Program To Convert A Lower Case To Upper Case
Here we will see two programs for lowercase to uppercase conversion. The first program converts a lowercase character to uppercase and the second program converts lowercase strings to uppercase strings.
Convert A Lower Case To Upper Case In C++
ASCII value of lowercase char a to z ranges from 97 to 122
ASCII value of uppercase char A to Z ranges from 65 to 92
For the conversion we are subtracting 32 from the ASCII value of input char.
#include <iostream>
using namespace std;
int main()
{
char ch;
cout<<"Enter a character in lowercase: ";
cin>>ch;
ch=ch-32;
cout<<"Entered character in uppercase: "<<ch;
return 0;
}
Enter a character in lowercase: q
Entered character in uppercase: Q
In this program user is asked to enter a string and then the program converts that input string into an uppercase string.
Logic used here: Looping through all the characters of the input string and checking whether the character lies in the ASCII range 97 to 122 (all the lowercase chars lies in this range). If the character is found to be in this range then the program converts that character into an uppercase char by subtracting 32 from the ASCII value.
#include <iostream>
#include <string>
using namespace std;
int main()
{
char s[30];
int i;
//display a message to user to enter the string
cout<<"Enter the String in lowercase: ";
//storing the string into the char array
cin>>s;
/* running the loop from 0 to the length of the string
* to convert each individual char of string to uppercase
* by subtracting 32 from the ASCII value of each char
*/
for(i=0;i<=strlen(s);i++) {
/* Here we are performing a check so that only lowercase
* characters gets converted into uppercase.
* ASCII value of a to z(lowercase chars) ranges from 97 to 122
*/
if(s[i]>=97 && s[i]<=122)
{
s[i]=s[i]-32;
}
}
cout<<"The entered string in uppercase: "<<s;
return 0;
}
Enter the String in lowercase:
STUDYTONIGHT
The entered string in uppercase:
studytonight
Conclusion
Here, in this tutorial, we have seen two programs for lowercase to uppercase conversion.