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.
To convert a given string to lowercase in C language, iterate over characters of the string and convert each character to lowercase using tolower() function.
The tolower() function takes an uppercase alphabet and convert it to a lowercase character. If the arguments passed to the tolower() function is other than an uppercase alphabet, it returns the same character that is passed to the function. It is defined in ctype.
C++ String has got built-in toupper() function to convert the input String to Uppercase.
In the C Programming Language, the tolower function returns c as a lowercase letter.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With