Program to Convert string to lowercase or uppercase in C++

Written by

Juhi Kamdar

In cases, when the whole string needs to be converted to upper case or lower case, we can use these methods for the conversion:

  1. Changing the ASCII code of all characters.
  2. Using functions: toupper and tolower

Another interesting way would be to convert uppercase letters to lower case letters and lower one to upper. It is also given below, as an extra segment.

Method 1: Changing the ASCII code of all characters

Logic:

The difference between the first character of uppercase and ASCII code of lower case character is 32, We add 32 to upper case letters in order to convert them to lower case, and we subtract 32 from lower case letters to convert them to upper case.

Algorithm to convert uppercase to lowercase:

  1. Check if the character is between A and Z i.e. it is a capital letter,
  2. If the character is a capital, we add 32 to it.
  3. Else, the character is already in lower case. Do nothing.

Algorithm for lowercase to uppercase:

  1. Check if the character is between ‘a’ and ‘z’ i.e. it is a lower case letter.
  2. If the character is a lower case letter, we subtract 32 from it.
  3. Else, the character is already in upper case. Do nothing.

Code:

#include <iostream>
using namespace std;

void lower_string(string str)
{
	for(int i=0;str[i]!='\0';i++)
	{
		if (str[i] >= 'A' && str[i] <= 'Z')    //checking for uppercase characters
			str[i] = str[i] + 32;         //converting uppercase to lowercase
	}
	cout<<"\n The string in lower case: "<< str;
}

void upper_string(string str)
{
	for(int i=0;str[i]!='\0';i++)
	{
		if (str[i] >= 'a' && str[i] <= 'z')   //checking for lowercase characters
			str[i] = str[i] - 32;        //converting lowercase to uppercase  
	}
	cout<<"\n The string in upper case: "<< str;
}

int main()
{
	string str;
    cout<<"Enter the string ";
    getline(cin,str);
    lower_string(str);       //function call to convert to lowercase
	upper_string(str);   //function call to convert to uppercase
	return 0;
}

Output:

Enter the string Hola Amigos!
The string in lower case: hola amigos!
The string in upper case: HOLA AMIGOS!

Method 2 : Using toupper() and tolower()

Logic:

The predefined method toupper() and tolower() takes an integer as input.

It returns the same character converted, according to the upper or lower method used.

This method works in the similar manner to the first method. Difference is that, it is already defined in the library, so, the user does not need to write the same code each time he/she wants to convert the string.

Algorithm :

  1. Input the string, using the getline() method.
  2. We run a for loop, take each character of given string one by one.
  3. Next, we pass the character in toupper() or tolower() and store it in the same place of the string.
  4. Output the converted string.

Code:

#include <iostream>
#include<string>

using namespace std;

int main() {
	string str;
    cout<<"Enter the string ";
    getline(cin,str);

	for(int i=0;i<str.length();i++)
		str[i]=toupper(str[i]);
	
    cout<<"\n The string in upper case:"<<str<<"\n";
		
	for(int j=0;j<str.length();j++)
		str[j]=tolower(str[j]);
	
    cout<<"The string in lower case: "<<str<<"\n";
}

Output:

Enter the string: studymite
The string in upper case:STUDYMITE
The string in lower case: studymite

Extra Segment: Upper to lower, and lower to upper(toggle case):

Logic:

We use the logic of method 1, here.

Algorithm:

  1. Input string
  2. Check if it is a capital letter
  3. If yes, convert it to small
  4. If no(it is a small letter), convert it to capital.

Code:

#include <iostream>
using namespace std;

int main()
{
	string str;
    cout<<"Enter the string ";
    getline(cin,str);
	for(int i=0;str[i]!='\0';i++)
	{
		if (str[i]>=65 && str[i]<=90 )          //checking for uppercase characters
			str[i] = str[i] + 32;          //converting uppercase to lowercase
		else if (str[i]>=97 && str[i]<=122 )
		    str[i] = str[i] - 32;              //converting lowercase to uppercase  
	}
	
	/*
	YOU CAN ALSO USE THIS METHOD  :-)
	for(int i=0;str[i]!='\0';i++)
	{
    	if (str[i]>=65 && str[i]<=90 )                  //checking for uppercase characters
			str[i] = toupper(str[i]);       //converting uppercase to lowercase
		else if (str[i]>=97 && str[i]<=122 )    //checking for lowercase characters
			str[i] = tolower(str[j]);       //converting lowercase to uppercase  
	}*/
	cout<<"\n The converted string: "<< str;
    return 0;
}

Output:

Enter the string: ThIs Is ToGgLe CaSe
The converted string: tHiS iS tOgGlE cAsE
Program to Convert string to lowercase or uppercase in C++