Convert each character of a string to uppercase in C++
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <boost/algorithm/string.hpp> using namespace std; int main() { string s = "boost library"; boost::to_upper(s); cout << s << endl; return 0; } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <iostream> #include <cctype> #include <algorithm> using namespace std; struct convert { void operator()(char &ch) { ch = toupper(static_cast<unsigned char>(ch)); } }; void to_upper(char &ch) { ch = toupper(static_cast<unsigned char>(ch)); } int main() { // 1. for_each + unary function string s = "function"; for_each(s.begin(), s.end(), to_upper); cout << s << endl; // 2. for_each + object of a class implementing ()operator s = "class object"; for_each(s.begin(), s.end(), convert()); cout << s << endl; // 3. for_each + lambda expression (C++11) s = "lambdas"; for_each(s.begin(), s.end(), [](char &ch) { ch = ::toupper(static_cast<unsigned char>(ch)); }); cout << s << endl; return 0; } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include <iostream> #include <algorithm> #include <cctype> using namespace std; struct convert { unsigned char operator()(unsigned char const &ch) { return toupper(ch); } }; int main() { // 1. for_each + unary function string s = "function"; transform(s.begin(), s.end(), s.begin(), ::toupper); cout << s << endl; // 2. for_each + object of a class implementing ()operator s = "class object"; transform(s.begin(), s.end(), s.begin(), convert()); cout << s << endl; // 3. for_each + lambda expression (C++11) s = "lambdas"; transform(s.begin(), s.end(), s.begin(), [](unsigned char const &ch) { return ::toupper(ch); }); cout << s << endl; return 0; } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> #include <cctype> using namespace std; char to_upper(char ch) { if (ch >= 'a' && ch <= 'z') { return ch - 32; } return ch; } int main() { string s = "naive solution"; for (char &ch: s) { ch = to_upper(ch); } cout << s; return 0; } |
Output:
NAIVE SOLUTION
That’s all about converting each character of 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 :)