Reading scandinavian letters in strings
Hello,
I must write a program that recognise scandinavian letters.
I wonder why this code does not work? (I use CLion 2018.)
1 2 3 4
|
for (char c: rad) {
if(c == 'å')
cout<<"Your text contains a " <<c<<'\n';
}
|
I used the ASCII table [url="
https://www.ascii-codes.com/cp865.html"]for scandinavian characters[/url] and tried:
[code]
for (unsigned char c: rad) {
if(c == 134)
cout<<"Your text contains a " <<c<<'\n';
}
[\code]
This does not work either. How can I solve it?
And how can I include all weird characters at once (once the initial problem is fixed)?
Could it be possible to write something like that?:
[code]
for (unsigned char c: rad) {
if(c >= 134 && c <= 165)
cout<<"Your text contains a " <<c<<'\n';
}
[\code]
Thank you.
Assuming that the strings you are dealing with are encoded in utf8, this will print the characters that are outside of the normal "ascii" range.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
#include <string>
int main() {
std::string s = "aäbcådeåxöhdøw";
for (size_t i = 0; i < s.size(); ) {
unsigned char c = s[i];
if (c < 0xC0)
i++;
else {
std::cout << s[i++];
if (c >= 0xC0) std::cout << s[i++];
if (c >= 0xE0) std::cout << s[i++];
if (c >= 0xF0) std::cout << s[i++];
std::cout << '\n';
}
}
}
|
Thank you, I get the principle I think...
Topic archived. No new replies allowed.