In this C++ tutorial, you will learn how to convert a given string to lowercase using transform() function, with example programs.

Convert String to Lowercase in C++

transform() function can be used to convert a given string to lowercase.

transform() function can apply a transformation of “to lowercase” for each character in the given string.

Syntax

The syntax of transform() function to convert a string str to lowercase string is

transform(str.begin(), str.end(), str.begin(), ::tolower);
ADVERTISEMENT

Examples

1. Convert string to lowercase

In the following program, we take a string: str and convert this string to lowercase using transform() function.

C++ Program

#include <iostream>
using namespace std;

int main() {
    string str = "HELLO WORLD";
    transform(str.begin(), str.end(), str.begin(), ::tolower);
    cout << str << endl;
}

Output

hello world
Program ended with exit code: 0

2. Convert string to lowercase when the letters are already lowercase

If any of the characters in the given string are already in lowercase, they are left as is.

C++ Program

#include <iostream>
using namespace std;

int main() {
    string str = "Hello World";
    transform(str.begin(), str.end(), str.begin(), ::tolower);
    cout << str << endl;
}

Output

hello world
Program ended with exit code: 0

Conclusion

In this C++ Tutorial, we learned how to convert a string to lowercase, with the help of example programs.