C++

Lowercase To Uppercase Conversion In C++ – Detailed Instructions

Here, we’ll look at two C++ programs that convert uppercase to lowercase letters. Both the input uppercase character in the first program and an uppercase string in the second program will be converted into lowercase.

Program to change uppercase characters to lowercase characters, for example

The uppercase character entered into this application is changed to a lowercase character. The reasoning behind this is that there is a 32-bit ASCII value difference between upper- and lowercase characters. Every lowercase character has an ASCII value of +32 that corresponds to its corresponding uppercase character. For instance, the ASCII value of the character “B” is 66 while the value of the character “b” is 98.
When the user enters an uppercase character, we add the number 32 to it to make it a lowercase character.

#include <iostream>
using namespace std;

int main()
{
	char ch;
	cout<<"Enter a character in uppercase: "; 
        cin>>ch;
	//converting the uppercase char to lowercase by adding 32
	//to the ASCII value of the input character
	ch=ch+32;
	cout<<"Entered character in lowercase: "<<ch;
	return 0;
}

Output:
C++ uppercase to lowercase conversion

Program to change Uppercase String to Lowercase String, Example 2

In this application, the user must enter the string (which may be all capital letters or a mix of letters), and the program will then change the string to all lowercase letters.

#include <iostream>
#include <string>
using namespace std;

int main()
{
   char s[20];
   int i;
   //display a message to user to enter the string
   cout<<"Enter the String in uppercase: "; 
   //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 lowercase
    * by adding 32 to the ASCII value of each char
    */
   for(i=0;i<=strlen(s);i++) { 
      /* Here we are performing a check so that only those 
       * characters gets converted into lowercase that are 
       * in uppercase. 
       * ASCII value of A to Z(uppercase chars) ranges from 65 to 92 
       */ 
      if(s[i]>=65 && s[i]<=92)
      {
	  s[i]=s[i]+32;
      }
   }
   cout<<"The entered string in lowercase: "<<s;
   return 0;
}

Output:
Uppercase String to lowercase in C++

Related Articles

Leave a Reply

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

Back to top button