Table of Contents
In this post, we will see how to convert String to lowercase in C++.
We can assume strings to be an array of characters. Each character has a specific ASCII value, and characters with different cases, for example, a and A have different ASCII values.
Ways to Convert a String to Lowercase in C++
We will discuss different methods on how to convert a string to all lowercase characters in C++.
Using the tolower()
and transform()
function
The tolower()
function in C++ returns a given character in lowercase. This function is defined in the cctype
header file. We can apply a given function to all elements of an array using the std::transform()
function. The transform()
is defined in the algorithm
header file.
Using this method, we will apply the tolower()
function to every character of a string.
See the following example.
1 2 3 4 5 6 7 8 9 10 11 |
#include <algorithm> #include <iostream> #include <cctype> #include <string> int main(){ std::string s = "BLOG"; std::transform(s.begin(), s.end(), s.begin(), ::tolower); std::cout << s; } |
Output:
In the above example, we apply the tolower()
function to every character of the string s
using the std::transform()
function and display it.
We can also eliminate the use of the tolower()
function by creating our own function that converts a character to lowercase. This function will use the ASCII value to return the corresponding lowercase of a character.
For example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <algorithm> #include <iostream> #include <cctype> #include <string> char fun(char i) { if (i <= 'Z' && i >= 'A') return i - ('Z' - 'z'); return i; } int main(){ std::string s = "BLOG"; std::transform(s.begin(), s.end(), s.begin(), fun); std::cout << s; } |
Output:
In the above example,
- The
fun()
function converts a given character to lowercase. - We apply the
fun()
function to every character using thetransform()
function.
Using the for
loop
We can use the for
loop to iterate over the string and convert each character to lowercase. To carry out the conversion, we can either use the tolower()
function or the user-defined method used in the previous example.
Both are demonstrated below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <algorithm> #include <iostream> #include <cctype> #include <string> char fun(char i) { if (i <= 'Z' && i >= 'A') return i - ('Z' - 'z'); return i; } int main(){ std::string s = "BLOG"; for(auto i : s) std::cout << char(tolower(i)); std::cout<< "\n"; for(auto i : s) std::cout << fun(i); } |
Output:
blog
In the above example,
- We used the
for
loop to traverse through the string. - Every character was converted to lowercase and displayed.
- In the first loop, we used the
tolower()
function. In this case, it will return the ASCII value, so we convert it to a character using thechar()
function. - In the second loop, we used the
fun()
function used in the earlier example.
Using the boost
library
The boost
library in C++ provides two methods to convert a given string to lowercase. The to_lower()
function from this library modifies the original string and converts it into lowercase. The to_lower_copy()
function creates a copy of the string and converts it to lowercase.
We will use both these functions below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <boost/algorithm/string.hpp> #include <string> #include <iostream> int main() { std::string s = "BLOG"; std::string s2 = boost::algorithm::to_lower_copy(s); std::cout << s2 << "\n"; boost::algorithm::to_lower(s); std::cout << s; } |
Output:
blog
In the above example,
- We use the
to_lower_copy()
function to convert the string to lowercase and store it in a new string. - The
to_lower()
function modifies the original string.
That’s all about how to covnert String to lowercase in C++.