C++ program to change the case of all characters in a string

Introduction :

In this C++ tutorial, we will learn how to change the case of all characters of a user provided string. It will take the string as an input from the user.

With this program, you will learn how to read a string as an input, how to iterate through the characters of a string, how to check the case of a character and how to change the case of a character.

Before showing you the program, let me show you the functions we are going to use :

islower() :

This function is used to check if a character is lowercase or not. It is defined as below :

int islower ( int c );

It returns zero if the result is false, any other number otherwise.

toupper() :

This function is used to convert a lowercase letter to uppercase. It is defined as below :

int toupper ( int c );

It takes the character to be converted as the parameter and cast it to an integer. The return value is the uppercase equivalent of c. It returns int representation of the uppercase if it exists. Else, it returns the integer representation of the same argument character.

tolower() :

This function is used to convert an uppercase letter to lowercase. It is defined as below :

int tolower ( int c );

Similar to toupper, it converts c to its lowercase equivalent and returns the value as an integer.

Both toupper and tolower method return the integer representation of a character. We can implicitly cast it to a character.

C++ program :

#include <iostream>
using namespace std;

int main()
{
    //1
    char str[100];

    //2
    cout << "Enter a string :" << endl;
    cin.get(str, 100);

    //3
    for (int i = 0; str[i] != '\0'; i++)
    {
        //4
        if (islower(str[i]))
        {
            str[i] = char(toupper(str[i]));
        }
        else
        {
            str[i] = char(tolower(str[i]));
        }
    }
    //5
    cout << "Final string " << str << endl;
}

Explanation :

The commented numbers in the above program denote the step numbers below :

  1. str is a character array to hold the user input string.

  2. Ask the user to enter a string. Read the string and store it in str variable using cin.get() method.

  3. Run one for loop to iterate through the characters of the string. The end character of a string is \0. This loop will iterate through the characters starting i = 0. It will stop only if str[i] is equal to ‘\0’.

  4. Inside the loop, we are checking for each character if it is a lower case or not. islower function is used to check if the character is lower case. If it is a lowercase character, change it to an upper case using the toupper function. Else, convert it to lower case using tolower function. We are also changing the character of the string str once its case is changed. toupper and tolower function return the integer representation of a character. We are using the char function to change it to a character.

  5. Finally, print out the final string to the user.

c++ change case of character in string

Sample Output :

Enter a string :
Hello World
Final string hELLO wORLD

Enter a string :
ThiS is A sAmPlE StRiNg
Final string tHIs IS a SaMpLe sTrInG

Conclusion :

In this tutorial, we have learned how to change the character case in a string in C++. Try to run the example program we have seen in this tutorial and drop one comment below if you have any queries.

Similar tutorials :