4

I am trying to apply lower function on non-ASCII characters. Following code doesn't work in Linux(Ubuntu) environment but works in windows.

int main() { std:string data="ŽŠ"; std::transform(data.begin(), data.end(), data.begin(), ::tolower); cout<< data << endl; return 0; } 

I tried installing language packs but did not work.Can someone help me what am i missing in this code?

2
  • Have you tryed locale changing? Commented Jun 13, 2016 at 21:49
  • It isn't a good idea to create string literals using non-ASCII characters and have these strings in the source file. Create literals using the correct escape sequences that represent the string. Commented Jun 13, 2016 at 21:58

1 Answer 1

5

::tolower() relies in the current locale set in the C library. The default "C" locale is only guaranteed to handle ASCII characters. Microsoft is likely using a different default locale that matches the user's current locale. That would explain why the code is able to work on Windows.

Use ::setlocale() to set the desired locale for ::tolower() to use. Otherwise, use a portable Unicode library, such as ICU.

Sign up to request clarification or add additional context in comments.

4 Comments

As the string is written directly in the .cpp file - could the issue be related to an encoding mismatch between file's one and expected one of compiler, too?
@Aconcagua: yes, that is also an issue to watch out for when using narrow strings.
I tried setting locale std::setlocale(LC_ALL,"de_DE"); and used "cin" rather than writing directly in file but still doesn't work. My OS locale was "en_US" in both linux and windows.
setlocale() only affects C functions, not STL classes. std::cin, std::cout, and other STL streams have their own locale handling. See the std::locale class and the std::basic_ios::imbue() method.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.