Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tolower function for C++ strings

Tags:

c++

tolower

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.

like image 828
brett Avatar asked Aug 04 '10 08:08

brett


People also ask

Can you use tolower on a string in c?

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.

How does tolower work in c?

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.

Does toupper work on strings?

C++ String has got built-in toupper() function to convert the input String to Uppercase.

What does tolower return in c?

In the C Programming Language, the tolower function returns c as a lowercase letter.


2 Answers

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.

like image 102
ereOn Avatar answered Oct 12 '22 02:10

ereOn


std::transform(myString.begin(), myString.end(), myString.begin(), std::tolower);
like image 20
TortoiseTNT Avatar answered Oct 12 '22 00:10

TortoiseTNT