string: how to count characters, not chars?

I'm trying to count the number of characters (not chars!) within a given string. Here is the simple program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

using namespace std;

int main()
{
  string str1, str2;

  str1 = "Münster";
  str2 = "Munster";
  cout << "length of " << str1 << ": " << str1.length()
       << " - length of " << str2 << ": " << str2.length() << endl;
  return 0;
}

the unexpected output is:
 
length of Münster: 8 - length of Munster: 7


Question: what is the easiest way to get in both cases the same result of 7?
How do I access character 2 of str1, which seems to be represented with 2 chars?

Thanks in advance for your support!
That's depend on the encoding. I guess the above is UTF-8 ( which has variable length for non-ASCII characters )
You can determine the length of each character based on the first few bits of the first byte:
http://en.wikipedia.org/wiki/UTF-8#Design
It looks like that your compiler saves the source files as UTF-8 (under linux it's pretty much standard).

UTF-8 is ok you can use http://utfcpp.sourceforge.net/ to deal with it.

If you just want to deal with umlauts (and/or other western europe special characters) you might switch to ISO 8859(-15). There any character represents one byte.
Topic archived. No new replies allowed.