How to convert std::string to lower case in C++?

C++Server Side ProgrammingProgramming

In this section, we will see how to convert all letters of a C++ string to lowercase letters. To do this thing we have to use the transform function. This transform function is present in the algorithm library.

The transform function takes the beginning pointer of the string and the ending pointer of the string. It also takes the beginning of the string to store the result, then the fourth argument is ::tolower. This helps to convert the string into a lowercase string. We can use this same method if we want to convert some string into an uppercase string.

Example Code

 Live Demo

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

int main() {
   string my_str = "Hello WORLD";

   cout << "Main string: " << my_str << endl;
   transform(my_str.begin(), my_str.end(), my_str.begin(), ::tolower);

   cout << "Converted String: " << my_str;
}

Output

Main string: Hello WORLD
Converted String: hello world
raja
Published on 01-Apr-2019 14:24:05
Advertisements