0

i wish to write a string in reverse order , i get string with cin and iterating it via for loop from strings length to 0. Problem is when i take turkish char it writes wrongly and also 1 turkish char increases strings length by 2 (i.e. ömür has length 6)

string text = "ömür";
for ( int i = text.length() ; i >= 0; i--)
{
    if(!isspace(text[i]) && text[i] != '\0')
    {
        cout<<text[i];
    }
}

expected output = rümö => what i get = r??m??

hkn
  • 371
  • 1
  • 14
  • Did you try using `std::wstring`? – Algirdas Preidžius Dec 08 '17 at 12:44
  • See https://stackoverflow.com/a/55650/50902 – kmkaplan Dec 08 '17 at 12:48
  • For a UTF-8 encoded Unicode string, a `char` holds an encoding unit, not (necessarily) a character. In order to print out the reverse of the string, you need to not reverse the multi-`char` encoding units. So your logic needs to find runs of encoding units. To make matters more interesting, some graphemes are expressed in multiple Unicode code points. Your code needs to handle those sequences of combining Unicode characters too. IBM has a very nice library to do these things (see link in kmkaplan's comment). – Eljay Dec 08 '17 at 12:56
  • 1
    Even for an ASCII char `for ( int i = text.length() `would acess the string out of bounds. –  Dec 08 '17 at 13:02
  • hımm when i tried on windows devC++ it works perfectly but in linux its not – hkn Dec 08 '17 at 13:08

1 Answers1

1

The problem is that nowdays, non ASCII characters take up more than one byte (C++ char). Your best bet is to use a library such as ICU that will sort out the Unicode stuff for you. You could then do:

#include <unicode/unistr.h>
#include <unicode/ustream.h>
#include <iostream>

int
main(int argc, char **argv)
{
    icu::UnicodeString text("ömür");
    text.reverse();
    std::cout << text;
}
kmkaplan
  • 18,655
  • 4
  • 51
  • 65