Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching russian vowels in C++

Tags:

c++

cyrillic

I wanted to write a function which returns true if a given character is a russian vowel. But the results I get are strange to me. This is what I've got so far:

#include <iostream>

using namespace std;

bool is_vowel_p(char working_char)
// returns true if the character is a russian vowel
{
    string matcher = "аяё×эеуюыи";

    if (find(matcher.begin(), matcher.end(), working_char) != matcher.end())
        return true;
    else
        return false;
}


void main()
{
    cout << is_vowel_p('е') << endl; // russian vowel
    cout << is_vowel_p('Ж') << endl; // russian consonant

    cout << is_vowel_p('D') << endl; // latin letter
}

The result is:

1
1
0

what is strange to me. I expected the following result:

1
0
0

It's seems that there is some kind of internal mechanism which I don't know yet. I'm at first interested in how to fix this function to work properly. And second, what is going on there, that I get this result.

like image 418
beyeran Avatar asked May 16 '13 14:05

beyeran


1 Answers

string and char are only guaranteed to represent characters in the basic character set - which does not include the Cyrillic alphabet.

Using wstring and wchar_t, and adding L before the string and character literals to indicate that they use wide characters, should allow you to work with those letters.

Also, for portability you need to include <algorithm> for find, and give main a return type of int.

like image 52
Mike Seymour Avatar answered Oct 25 '22 12:10

Mike Seymour