Reputation: 101
I have been struggling to get a simple function to work, the purpose is to pass in a character, the function then search's the ALPHABET string and then if the character is found, returns the index of the character within the string .
I have a basic constant containing the alphabet, including an apostrophe and space character:
const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,’ ";
Now I have a function which takes a character as an argument:
int charaPosition(string chara){
transform(chara.begin(), chara.end(),chara.begin(), ::toupper);
int charaIndex = ALPHABET.find(chara, 0);
if (charaIndex != string::npos) {
return charaIndex;
}else{
cout << "Not found" << endl;
}
}
Function is initiated like so:
cout << charaPosition("s") << endl;
It all works fine up to a certain point, if I pass in A, the function returns 0, B return 1 etc. However if I pass in the space character, it returns 31 rather than 29, and if I pass in the apostrophe character, it just doesn't get found.
I would be very grateful for anyones help.
Upvotes: 2
Views: 211
Reputation: 32240
Your problem is that ’
is not ASCII, it's Unicode. It occupies more than 1 byte in your string.
It all works fine up to a certain point, if I pass in A, the function returns 0, B return 1 etc
: Right, up to that point, it's all ASCII, so you have 1 byte per character, therefore all resulting indices make sense;
However if I pass in the space character, it returns 31 rather than 29
: The problem is that ’
occupies exactly 3 bytes (226 128 153) rather than 1 byte, because it's Unicode. You should use std::wstring
and wchar_t
to make this work properly, or replace that character by '
(ASCII 39);
and if I pass in the apostrophe character, it just doesn't get found.
: That apostrophe is not ASCII, so you're searching for multiple bytes instead of a single one.
Upvotes: 1