Reputation: 1393
When I tried tolower() with non english charecters in c++ it's not working normally. I searched that issue and I came across something about locale but I am not sure about best solution of that.
My sample code is below:
printf("%c ",tolower('Ü'));
Upvotes: 4
Views: 1175
Reputation: 15768
Unfortunately, the standard C++ library does not have sufficient support for changing the case of all possible non-English characters (in so far as those characters that have case variants at all). This limitation is caused by the fact that the C++ standard assumes that a single character and its case variants occupy exactly one char
object (or wchar_t
object for wide characters) and for non-English characters that is not guaranteed to be true (also depending on how the characters are coded).
If your environment uses a single-byte encoding for the relevant characters, this might give you what you want:
std::cout << std::tolower('Ü', locale());
With wide characters, you will probably have more luck:
std::wcout << std::tolower(L'Ü', locale());
but even that won't give the correct result for toupper(L'ß')
, which would be the two-character sequence L"SS"
).
If you need support for all characters, take a look at the ICU library, in particular the part about case mappings
Upvotes: 6
Reputation: 545905
Like Bart has shown, C++ simply doesn’t like multi-byte encodings. Luckily, you can use Boost.Local to solve this without too much hassle. Here’s a simple example:
#include <iostream>
#include <locale>
#include <boost/locale.hpp>
int main() {
boost::locale::generator gen;
std::locale loc = gen("en_US.UTF-8");
std::string line;
while (std::getline(std::cin, line))
std::cout << boost::locale::to_lower(line, loc) << '\n';
}
To compile, we need to link to the Boost.Locale library:
g++ -lboost_locale lower.cpp -o lower
And when we execute it, we get the following:
$ ./main <<< 'ICH HÄTTE GERNE EINEN SÜßEN HASEN'
ich hätte gerne einen süßen hasen
Upvotes: 3