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

Convert String to Uppercase in C++

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

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

Syntax

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

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

Examples

1. Convert string to uppercase

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

C++ Program

#include <iostream>
using namespace std;

int main() {
    string str = "hello world";
    transform(str.begin(), str.end(), str.begin(), ::toupper);
    cout << str << endl;
}

Output

HELLO WORLD
Program ended with exit code: 0

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

If any of the characters in the given string are already in uppercase, 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(), ::toupper);
    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 uppercase, with the help of example programs.