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.
In this tutorial, we will learn the syntax and how to use transform() function to convert given string to uppercase, with example programs.
Syntax
The syntax of transform() function to convert a string str
to uppercase string is
transform(str.begin(), str.end(), str.begin(), ::toupper);
Examples
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
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.