Convering a string into upper or lower case in C++

Written by

Ananya Pandey

By now, you must know about the string data type. Despite being an array of characters, strings can be effectively manipulated using functions defined by the user or the Standard library of C++ Several functions like strlen( ), strcmp( ), strcpy( ), are some commonly used functions used to manipulate strings. 

In this article, we aim to discuss functions like toupper( ) and tolower( ) so as to convert a given string into uppercase or lowercase letters.

Several approaches can be followed to convert a string of characters into upper or lower case.

Approach 1: Using Library functions

Functions like toupper( ) and tolower( ) are a part of the ctype.h header file but can conveniently be used with the header file now.

The toupper( ) function converts a string of characters to upper case characters.

The tolower( ) function converts a string of characters to lower case characters.

A point to keep in mind is that these functions cannot simply convert an entire string of words or characters. They can only take one character at a time.

Algorithm

Step 1: Enter a string using the getline( ) method.

Step 2: Run a for or while loop to iterate over the string, character to character.

Step 3: Convert the character into upper and lower case using toupper( ) and 

 tolower( ) functions respectively.

Step 4: Print the resultant string.

Code

Output:

Enter the string:  Good morning all!

The string in upper case: GOOD MORNING ALL!

The string in lower case: good morning all!

Approach 2: Changing the ASCII value of all the characters

The American Standard Code for Information Interchange (ASCII) assigns integer values to the character or ‘char’ data set. The ASCII value of the upper case letters range from 65 to 90 and that of lower case letters start from 97 until 122.

As you may notice, the difference between every upper and lower case character is exactly 32.

Hence, 32 to can be added or subtracted to convert a character into the alternate case.

For instance, the ASCII code for ‘A’ is 65 and in order to convert it into its lowercase version ‘a’, 32 is added to 65 which results in 97- the ASCII value of ‘a’.

Algorithm:

Step 1: Enter a string of any length using getline( ).

Step 2: Define two functions to convert the input string in upper or lower case using a                                                    while/for loop.  

Step 3: Define lower_str( ) and check if the character is in the upper case.

Step 4: If true, add 32 to it.

Step 5: If false, the character is already in the lower case. Do nothing.

Step 6: Define upper_str( ) and check if the character is in the lower case.

Step 7: If true, subtract 32 from it.

Step 8: If false, the character belongs to the upper case already. Do nothing.

Step 9: Call both the methods to display the output string.

Code:

#include

#include

using namespace std;

void lower_str(string str)

{

for(int i=0;i<=str.length();i++)

{

if (str[i] >= ‘A’ && str[i] <= ‘Z’)    

str[i] = str[i] + 32;   //converting upper tolowercase }

cout<<“\n The string in lower case: “<< str;

}

void upper_str(string str)

{

for(int i=0;str[i]!=‘\0’;i++)      //’\0’ stands for null

{

if (str[i] >= ‘a’ && str[i] <= ‘z’)   

str[i] = str[i] – 32;   //converting lower touppercase

}

cout<<“\n The string in upper case: “<< str;

}

int main()

{

string str;

      cout<<“Enter the string: “;

      getline(cin,str);

      lower_str(str);       //function call to convert to lowercase

      upper_str(str);   //function call to convert to uppercase

return 0;

}

Output: 

Enter the string:  Happy Monday!

The string in lower case:  happy monday!

The string in upper case:  HAPPY MONDAY!

NOTE: Instead of checking if a character belongs to the upper case or lower case of letters using the two if conditions mentioned above, you can easily use the library functions isupper( ) and islower( ) to carry out the same operation.

if (islower(str[i]))                 if (isupper(str[i]))                       

    str[i] = str[i] – 32;                str[i] = str[i] + 32;

There are multiple ways to present this code. You can also carry out the two conversions in just one function and in the same for loop as per your preference. Two functions are used in the above code to ensure ease of understanding.

BONUS: THE TOGGLE CASE

Now that you’ve learnt the above two approaches, the same can be applied for a different output.

The toggle case changes the capitalization or the case of any text quickly. It is basically used to shift between two case views i.e, upper or lower, and provide an alternate view of the string.

Though larger applications provide multiple customizations for its users to toggle their text,  

Here, we will convert the uppercase characters into the lowercase and vice-versa. Eg: Apple becomes aPPLE.

Algorithm:

Step 1: Input a string of any length.

Step 2: Use a for/while loop to traverse through the string character by character.

Step 3: If a character belongs to the uppercase, convert it into the lowercase.

Step 4: If a character belongs to the lowercase, convert it into the uppercase.

Step 5: Print the resultant string.

Code:

#include

#include

using namespace std;

void toggle_str(string str)

{

    for(int i=0;str[i]!=‘\0’;i++)

{

if (str[i]>=65 && str[i]<=90 )         

str[i] = str[i] + 32;          

else if (str[i]>=97 && str[i]<=122 )

    str[i] = str[i] – 32;              

}

    cout<<“\n The converted string: “<< str;

}

int main()

{

string str;

    cout<<“Enter the string “;

    getline(cin,str);

    toggle_str(str);

    return 0;

}

Output:

Enter the string:  HP stands for Hewlett Packard

The converted string: hp STANDS FOR hEWLETT pACKARD

Alternatively, the toggle_str( ) method can be written in several ways with different approaches as mentioned above. 

Some of them are illustrated below.

  1. i) Using isupper( ) and islower( )

void toggle_str(string str)

{

    for(int i=0;str[i]!=‘\0’;i++)

{

if (isupper(str[i]) )         

str[i] = str[i] + 32;          

else if (islower(str[i]) )

    str[i] = str[i] – 32;              

}

    cout<<“\n The converted string: “<< str;

}

  1. ii) Using only library functions:

void toggle_str(string str)

{

    for(int i=0;str[i]!=‘\0’;i++)

{

if (isupper(str[i]) )         

str[i]= tolower(str[i]);         

else if (islower(str[i]) )

    str[i] = toupper(str[i]);             

}

    cout<<“\n The converted string: “<< str;

}

Convering a string into upper or lower case in C++