Convert a string to lowercase in C++
This post will discuss how to convert a string to lowercase in C++.
1. Using range-based for-loop
A simple approach is to create our own routine for converting a character to its lowercase version. The idea is to iterate the string using a range-based for-loop with a reference variable and call our conversion routine for each character.
|
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 |
#include <iostream> #include <string> #include <cctype> char to_lowercase(char c) { if (c >= 'A' && c <= 'Z') { return c + 32; } return c; } int main() { std::string str = "ABCD"; for (char &c: str) { c = to_lowercase(c); } std::cout << str; return 0; } |
Output:
abc
2. Using std::for_each function
A less verbose solution is to use the standard algorithm std::for_each, which uses a loop under the hood and applies a specified function to every string element. The specified function may be a unary function, a lambda expression, or an object of a class overloading the () operator.
We have used the std::tolower function in the following example, which converts the given character to lowercase. The argument should be converted to unsigned char; otherwise, the behavior is undefined to use it safely with signed chars.
|
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 <string> #include <algorithm> #include <cctype> struct convert { void operator()(char &c) { c = std::tolower(static_cast<unsigned char>(c)); } }; void to_lowercase(char &c) { c = std::tolower(static_cast<unsigned char>(c)); } int main() { // 1. for_each + unary function std::string str = "CONVERT"; std::for_each(str.begin(), str.end(), to_lowercase); std::cout << str << std::endl; // 2. for_each + object of a class implementing ()operator str = "STRING"; std::for_each(str.begin(), str.end(), convert()); std::cout << str << std::endl; // 3. for_each + lambda expression (C++11) str = "LOWERCASE"; std::for_each(str.begin(), str.end(), [](char &c) { c = ::tolower(static_cast<unsigned char>(c)); }); std::cout << str << std::endl; return 0; } |
Output:
convert
string
lowercase
3. Using std::transform function
Another good alternative is to use the STL algorithm std::transform, which applies an operation to elements of the specified range and stores the result in another range, which begins at the specified output iterator. The operation may be a unary operation function, a lambda expression, or an object of a class implementing the () operator.
|
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 <string> #include <algorithm> #include <cctype> struct convert { unsigned char operator()(unsigned char const &c) { return std::tolower(c); } }; int main() { // 1. for_each + unary function std::string str = "CONVERT"; std::transform(str.begin(), str.end(), str.begin(), ::tolower); std::cout << str << std::endl; // 2. for_each + object of a class implementing ()operator str = "STRING"; std::transform(str.begin(), str.end(), str.begin(), convert()); std::cout << str << std::endl; // 3. for_each + lambda expression (C++11) str = "LOWERCASE"; std::transform(str.begin(), str.end(), str.begin(), [](unsigned char const &c) { return ::tolower(c); }); std::cout << str << std::endl; return 0; } |
Output:
convert
string
lowercase
4. Using Boost Library
Finally, we can use boost::algorithm::to_lower to convert each element of std::string to lower case. We can also use boost::algorithm::to_lower_copy, but it returns a copy of the string object and doesn’t modify the given string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <string> #include <boost/algorithm/string.hpp> int main() { std::string str = "ABCD"; boost::to_lower(str); std::cout << str; return 0; } |
Output:
abcd
That’s all about converting a string to lowercase 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 :)