C++

Program In C++ That Changes Lowercase To Uppercase – Q&A

Here, we’ll look at two programs for changing lowercase to uppercase letters. Lowercase characters are converted to uppercase in the first program, while uppercase characters are converted from lowercase in the second program.

Example 1: A program that changes lowercase to uppercase characters

Lowercase characters from a to z have an ASCII value between 97 and 122.
Uppercase characters from A to Z have an ASCII value between 65 and 92.
For conversion, we deduct 32 from the input char’s ASCII value.

#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;
}

C++ convert lowercase char to uppercase

Example 2: A program to change a string from lowercase to uppercase

In this application, after the user enters a string, it is converted to an uppercase string by the program.
Here’s the reasoning: inspecting each character in the input string, in a loop, to see if it falls between the ASCII range of 97 to 122 (all the lowercase chars lies in this range). When a character is discovered to fall within this range, the computer changes it from a lowercase character to an uppercase one by taking 32 off 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;
}

Output:
Lowercase to Uppercase conversion in C++

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button