Convert a string to uppercase in C++
This post will discuss how to convert a string to uppercase in C++.
1. Using std::transform
The standard algorithm std::transform is commonly used to apply a given function to all the elements of the specified range. This function can be a unary operation, a lambda, or an object implementing the () operator. It can be used as follows to capitalize a string, with the toupper function in the std namespace.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <string> #include <algorithm> int main() { std::string str = "Hello World"; std::transform(str.begin(), str.end(), str.begin(), [](char const &c) { return std::toupper(c); }); std::cout << str << std::endl; // HELLO WORLD return 0; } |
The above code can be further shortened with the tolower function in the global namespace which can be directly referenced with a name. It can be accessed with ::toupper, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> #include <string> #include <algorithm> int main() { std::string str = "Hello World"; std::transform(str.begin(), str.end(), str.begin(), ::toupper); std::cout << str << std::endl; // HELLO WORLD return 0; } |
2. Using Boost
Alternatively, we can use the boost string algorithm to_lower to convert each character of the input sequence to lower case. It is available in the header <boost/algorithm/string.hpp>.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> #include <string> #include <boost/algorithm/string.hpp> int main() { std::string str = "Hello World"; boost::to_upper(str); std::cout << str << std::endl; // HELLO WORLD return 0; } |
The boost::algorithm::to_lower algorithm modifies the input sequence in-place. To avoid modifying the original string and return a copy instead, use boost::algorithm::to_lower_copy algorithm. It is available in the header <boost/algorithm/string.hpp>.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> #include <string> #include <boost/algorithm/string.hpp> int main() { std::string str = "Hello World"; std::string upper_str = boost::to_upper_copy<std::string>("Hello World"); std::cout << upper_str << std::endl; // HELLO WORLD return 0; } |
3. Using std::for_each
Another option is to iterate the string using std::for_each and convert each character to uppercase. This would translate to the following code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <string> #include <algorithm> int main() { std::string str = "Hello World"; std::for_each(str.begin(), str.end(), [](char& c) { c = toupper((unsigned char)c); }); std::cout << str << std::endl; // HELLO WORLD return 0; } |
The std::for_each algorithm is available since C++11. The behavior of this function is equivalent to:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> int main() { std::string str = "Hello World"; for (auto & c: str) { c = toupper(c); } std::cout << str << std::endl; // HELLO WORLD return 0; } |
That’s all about converting a string to uppercase in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)