This post will discuss how to convert each character of a string to uppercase in C++.

1. Using Boost Library

A simple and efficient solution is to use the boost::algorithm::to_upper to convert each character of std::string to uppercase.

Download Code

Output:

BOOST LIBRARY

 
We can also use boost::algorithm::to_upper_copy, which returns a “copy” of the string object in uppercase.

2. Using std::for_each function

If the boost library is not available, try using the standard algorithm std::for_each, which applies a given function object to every character of the string. The function needs to convert a character to uppercase in-place, as shown below:

Download  Run Code

Output:

FUNCTION
CLASS OBJECT
LAMBDAS

3. Using std::transform function

Another good alternative is to use the STL algorithm std::transform, which applies a function to the specified range and stores the result in another range, which begins at the specified output iterator.

Download  Run Code

Output:

FUNCTION
CLASS OBJECT
LAMBDAS

4. Naive solution

Finally, we can write our own function for converting characters of a string to uppercase. This is demonstrated below:

Download  Run Code

Output:

NAIVE SOLUTION

That’s all about converting each character of a string to uppercase in C++.