tolower function for C++ strings

brett picture brett · Aug 4, 2010 · Viewed 39.7k times · Source

Is there an inbuilt function to convert C++ string from upper case letters to lowercase letters ? If not converting it to cstring and using tolower on each char is the only option ?

Thank you very much in advance.

Answer

ereOn picture ereOn · Aug 4, 2010

If boost is an option:

#include <boost/algorithm/string.hpp>    

std::string str = "wHatEver";
boost::to_lower(str);

Otherwise, you may use std::transform:

std::string str = "wHatEver";
std::transform(str.begin(), str.end(), str.begin(), ::tolower);

You can also use another function if you have some custom locale-aware tolower.