D
D
Dmitry2014-08-28 21:22:09
C++ / C#
Dmitry, 2014-08-28 21:22:09

C++ how to count characters in a string?

C++ There is a variable of type String, it contains Russian and English letters, spaces and punctuation marks.
You need to count the number of characters in this string, including spaces and signs.
The problem is that when using length() or size() , the result is not accurate for some reason. Shows the correct number of characters for English letters and multiplied by 2 for Cyrillic.
How can this problem be solved?
Thank you!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim, 2014-08-28
@1diem

save the code in windows-1251 encoding, compile, run

X
xmoonlight, 2014-08-28
@xmoonlight

wstring_convert< codecvt_utf8<char32_t>, char32_t >().from_bytes(the_std_string).size()

M
Misha Krinkin, 2014-08-28
@kmu1990

well, probably the string uses some kind of utf-x, look at what you have, after that it's quite easy to count the characters.
UPDATE: if utf, then not some kind of course, but utf-8.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question