Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which tolower in C++?

Tags:

Given string foo, I've written answers on how to use cctype's tolower to convert the characters to lowercase

transform(cbegin(foo), cend(foo), begin(foo), static_cast<int (*)(int)>(tolower))

But I've begun to consider locale's tolower, which could be used like this:

use_facet<ctype<char>>(cout.getloc()).tolower(data(foo), next(data(foo), foo.size()));
  • Is there a reason to prefer one of these over the other?
  • Does their functionality differ at all?
  • I mean other than the fact that tolower accepts and returns an int which I assume is just some antiquated C stuff?
like image 427
Jonathan Mee Avatar asked May 27 '16 11:05

Jonathan Mee


People also ask

What C library is tolower?

Description. The C library function int tolower(int c) converts a given letter to lowercase.

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.

Where is tolower defined?

The tolower() function is defined in the ctype. h header file.

Does tolower work for char?

ToLower(Char)Converts the value of a Unicode character to its lowercase equivalent.


2 Answers

Unfortunately,both are equally bad. Although std::string pretends to be a utf-8 encoded string, non of the methods/function (including tolower), are really utf-8 aware. So, tolower / tolower + locale may work with characters which are single byte (= ASCII), they will fail for every other set of languages.

On Linux, I'd use ICU library. On Windows, I'd use CharUpper function.

like image 145
user3104201 Avatar answered Oct 29 '22 15:10

user3104201


In the first case (cctype) the locale is set implicitely:

Converts the given character to lowercase according to the character conversion rules defined by the currently installed C locale.

http://en.cppreference.com/w/cpp/string/byte/tolower

In the second (locale's) case you have to explicitely set the locale:

Converts parameter c to its lowercase equivalent if c is an uppercase letter and has a lowercase equivalent, as determined by the ctype facet of locale loc. If no such conversion is possible, the value returned is c unchanged.

http://www.cplusplus.com/reference/locale/tolower/

like image 43
Willi Mentzel Avatar answered Oct 29 '22 16:10

Willi Mentzel