C++ tolower example – Converting a String to Upper & Lower Case in C++ using STL & Boost Library

C++ tolower example: In the previous article, we have discussed about Program to Determine all Pythagorean Triplets in the Range in C++ and Python. Let us learn how to Convert a String to Upper & Lower Case in C++ Program.

Converting a String to Upper & Lower Case using STL & Boost Library in C++

C++ string to lower: We are going to see how we can convert the string into upper and lowercase in two ways i.e.

Convert a String to Upper Case using STL :

String to lower c++: We can convert the string to uppercase using the toupper( ) function, however the function works on character and not on the whole string so we have to iterate through all characters inside the string by passing it as a lambda function.

#include <iostream>
#include <string>
#include <algorithm>

int main(int argc, char const *argv[])
{
    //String to uppercase
    std::string str = "Example string";
    std::cout << "The initial string is ->" << str << std::endl;
    std::for_each(str.begin(), str.end(), [](char &ch)
                  { ch = ::toupper(ch); });
    std::cout << "The new string in upper case ->" << str;
    return 0;
}

Output :
The initial string is ->Example string
The new string in upper case ->EXAMPLE STRING

Convert a String to Lower Case using STL :

How to make a string lowercase c++: Just like the toupper( ) function, tolower( ) converts the string to lower case.

#include <iostream>
#include <string>
#include <algorithm>

int main(int argc, char const *argv[])
{
    //String to lowercase
    std::string str = "EXAMPLE String";
    std::cout << "The initial string is ->" << str << std::endl;
    std::for_each(str.begin(), str.end(), [](char &ch)
                  { ch = ::tolower(ch); });
    std::cout << "The new string in lower case ->" << str;
    return 0;
}
Output:
The initial string is ->EXAMPLE String
The new string in lower case ->example string

The downside of toupper( ) and tolower( ) functions are that, they can only convert one character at a time, to overcome that we need to use the boost library algorithm.

Convert a String to Upper Case and Lower Case using Boost Library :

Using tolower in c++: The boost algorithm library has functions boost::to_upper( ), boost::to_lower( ) to convert the string into upper or lower case.

#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>
int main()
{
    std::string str = "Boost library algorithm for STRING CONVERSION";
    // convert the string to upper case
    boost::to_upper(str);
    std::cout << "To upper case: " << str << std::endl;
    // Converts the string to lower case
    boost::to_lower(str);
    std::cout << "To lower case: " << str << std::endl;
}
Output :
To upper case: BOOST LIBRARY ALGORITHM FOR STRING CONVERSION
To lower case: boost library algorithm for string conversion

Creating a new string after case conversion :

C++ make string lowercase: While we can modify the string using boost::to_upper( ) and boost::to_lower( ), in case we need the original string we can use the boost::to_upper_copy() or boost::to_lower_copy() to copy the modified string and store the value into new variable.

#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>
int main()
{
    std::string str = "Boost library algorithm for STRING CONVERSION", newUpper, newLower;
    // convert the string to upper case
    newUpper = boost::to_upper_copy(str);
    std::cout << "To upper case: " << newUpper << std::endl;
    // Converts the string to lower case
    newLower = boost::to_lower_copy(str);
    std::cout
        << "To lower case: " << newLower << std::endl;
    std::cout << "The original string: " << str;
}
Output :
To upper case: BOOST LIBRARY ALGORITHM FOR STRING CONVERSION
To lower case: boost library algorithm for string conversion
The original string: Boost library algorithm for STRING CONVERSION