1

I wrote this function to make a string all uppercase. Unfortunately, it does not work with the extended ASCII characters, like ä, ö, ü, é, è and so on. How can I convert the string to uppercase and convert these characters also (Ä, Ö, Ü, É, È)?

void toUppercase1(string & strInOut)
{
  std::locale loc;
  for (std::string::size_type i=0; i<strInput.length(); ++i)
  {
    strInput.at(i) = std::toupper(strInput.at(i),loc);
  }
return;
}

This is the new version.

#include <clocale>
void toUppercase2(string& strInput)
{
  setlocale(LC_CTYPE, "sv_SE");

  for (std::string::size_type i=0; i<strInput.length(); ++i) {
    strInput.at(i) = toupper(strInput.at(i));
  }

  // reset locale    
  setlocale(LC_ALL, "");

  return;
}

Unfortunately, the above method toUppercase2(string&) is very slow. It seems that changing the locale globally requires some effort. I assume that using a locale object from C++ which is initialized once and then used by toupper is much faster, but I cannot find an example of how to use it properly.

Any hints to where I can find more information about locales and their use is appreciated.

Fabian
  • 4,001
  • 4
  • 28
  • 59
  • 1
    If you read a [`std::locale` constructor reference](http://en.cppreference.com/w/cpp/locale/locale/locale) you will see that the no-argument version will either use [the global locale](http://en.cppreference.com/w/cpp/locale/locale/global) or the [lcassic locale](http://en.cppreference.com/w/cpp/locale/locale/classic) (both can be the same. The classic locale is the old "C" locale which I doubt support anything but the standard 7-bit ASCII. The "high" extended ASCII characters really depend on you setting the proper locale as the extended characters depend on what locale you have. – Some programmer dude Mar 31 '15 at 08:20
  • 1
    As for how to fix your problem, you should probably create the locale object using the `"sv_SE"` locale (or something simlar considering the example characters you provide). – Some programmer dude Mar 31 '15 at 08:31

0 Answers0