Example assignment: char str[]="Это просто проверка программы." strlen(str) shows 56 characters (there are 30). cin.getline when typing the same line swallows 12 characters. Other functions do not work either. But when entering the Latin, everything is in order. Qt5.7 in windows works fine. Who can prompts a problem?

  • one
    Perhaps the case is in the encoding, and Cyrillic characters do not weigh one by one. - AivanF.
  • How do you enter a string? And where does Qt? - JK_Action
  • When typing from the keyboard - char str [35]; cin.getline (str, 35); Enter the line "This is just a program check." At the output cout << str; we get "This is just a check." I understand that the problem is in the encoding, but how can I change it? - Valeryi

1 answer 1

By default, in Ubuntu everything is UTF8 encoded. Russian characters in it occupy two bytes, and not one by one. strlen returns the length of the string in bytes, not in characters, so the answer is 56 (26 * 2 + 4 = 56) - correct.

  • That is, the string constants have UTF8 encoding by default when compiled under Ubuntu? - Cerbo
  • 2
    In Linux, the rule of good tone is to use utf-8 everywhere. Your string has the encoding in which you saved the file. Qt hopes and highly recommends using utf-8 in sorts. Instead of char str[] it is better to use QString . everything will be fine there. - KoVadim
  • 2
    @Cerbo, in any Linux files the default in utf-8, like the input encoding of gcc / clang, we kind of discussed it with you somehow. - ixSci
  • There are special amateurs who do a default Koi8 encoding (or even cp1251) and then post similar questions :) - KoVadim