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.
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.
If
boost
is an option:Otherwise, you may use
std::transform
:You can also use another function if you have some custom locale-aware
tolower
.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.I have an implementation I found it faster than std::transform , Compiled in g++ -03 Fedora 18. my example converts std::string
I will be glad to know if performance can be improved further.
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: