0

I want to print the first letter of a string.

#include <iostream>
#include <string>

using namespace std;

int main() {
  string str = "다람쥐 헌 쳇바퀴 돌고파.";
  cout << str.at(0) << endl;
}

I want '다' to be printed like java, but '?' is printed.

How can I fix it?

1 Answers1

2

That text you have in str -- how is it encoded?

Unfortunately, you need to know that to get the first "character". The std::string class only deals with bytes. How bytes turn into characters is a rather large topic.

The magic word you are probably looking for is UTF-8. See here for more infomation: How do I properly use std::string on UTF-8 in C++?

If you want to go down this road yourself, look here: Extract (first) UTF-8 character from a std::string

And if you're really interested, here's an hour-long video that is actually a great explanation of text encoding: https://www.youtube.com/watch?v=_mZBa3sqTrI

rand'Chris
  • 788
  • 4
  • 17