How to convert a string to lower or upper case in C++

1. Use std::transform and tolower or toupper

    #include <algorithm>
    #include <string>
    #include <cctype> 

    std::string& to_lower(std::string& str)
    {
        std::transform(str.begin(), str.end(), str.begin(), static_cast<int(*)(int)>(std::tolower));
        return str;
    }

    std::string& to_upper(std::string& str)
    {
        std::transform(str.begin(), str.end(), str.begin(), static_cast<int(*)(int)>(std::toupper));
        return str;
    }

    std::string str = "Chorlton-cum-Hardy"; 
    to_lower(str);
    to_upper(str);

2. Use boost::algorithm::to_lower or boost::algorithm::to_upper

    #include <boost/algorithm/string.hpp>    

    std::string str = "Adwick-le-Street";
    boost::algorithm::to_lower(str);
    boost::algorithm::to_upper(str);

References:
Function template to_lower
Function template to_upper

Related