How to Convert a C++ String to Uppercase?
In C++, converting a string to uppercase means we have to convert each character of the string that is in lowercase to an uppercase character. In the article, we will discuss how we can convert a string to uppercase in C++.
Example
Input: w3wiki Output: w3wiki
Converting String to Uppercase in C++
In C++, we can convert a string to uppercase using the toupper() function that converts a character to uppercase. We will apply this function to each character in the string using std::transform()
function.
C++ Program to Convert a String to Uppercase
C++
// C++ program to convert a string to uppercase #include <algorithm> // for using transform #include <cctype> // for using toupper #include <iostream> #include <string> using namespace std; int main() { string myStr = "w3wiki" ; cout << "Input String is: " << myStr << endl; // using transform with toupper to convert mystr to // uppercase transform(myStr.begin(), myStr.end(), myStr.begin(), :: toupper ); // printing string after conversion cout << "String after conversion to uppercase: " << myStr << endl; return 0; } |
Output
Input String is: w3wiki String after conversion to uppercase: w3wiki