hi,
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.
hi,
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.
There is no built-in function to do this, and doing it is surprisingly complicated, because of locales et al. If tolower does what you need, it may be your best bet.
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.
std::transform(myString.begin(), myString.end(), myString.begin(), std::tolower);
Like ereOn says:
std::transform(str.begin(), str.end(), str.begin(), std::tolower );
Or via for_each:
std::for_each(str.begin(), str.end(), std::tolower );
Transform is probably better of the two.
For this problem you can use the STL's transform method to solve it:
std::string str = "simple";
std::transform(str.begin(), str.end(), str.begin(), std::tolower);