How in C # "flip" the line: it was - 123, it became - 321?

    3 answers 3

    Something like this:

    using System; static class StringHelper { public static string ReverseString(string s) { char[] arr = s.ToCharArray(); Array.Reverse(arr); return new string(arr); } } 
    • Yes, exactly what you need, thank you - tehead
    • one
      oh well: "str" ​​.Reverse (). Aggregate (string.Empty, (acc, ch) => acc + ch); - Specter
    • In my case, readability is more important than performance, because there are enough long-term operations :) - tehead
    • four
      It can be shorter of course: string input = "hello world"; string output = new string (input.ToCharArray (). Reverse (). ToArray ()); - igumnov
    • new string(s.Reverse().ToArray());

    • All the methods that were offered to you (including mine) are not suitable for serious production of the code, because, for example, they do not take into account the placement of Acute Accents , which, when reversed, will come up with the wrong sign, and will also reverse the surrogate pairs in UTF-16 , in fact, breaking the correct unicode string.

    • More about these pitfalls - Jon Skeet, C# in Depth - 1.7.2.

    • Those. The correct algorithm is to first translate the string into the UCS array, reverse it, and then translate UCS into the string in the original encoding. - avp
    • @avp - Yes, you can do it the way you propose (in fact, in any Unicode format like UCS / UTF32 / ... , in which these elements are represented by one code point'ом ). - You can also make an iterator for code points, and not code units , which will reverse the surrogate pairs as a whole. Another thing is that the problem with accents still remains and to solve it in a general way is a non-trivial task. Plus, for sure, there are other problems that I do not know / did not think about. - Costantino Rupert
    • But aren’t diacritical marks included in UTF-32 / UCS? I always believed that for all these "signs with a hat" (or am I mistaken and "accented characters" is it something else?) There are Unicode characters. - avp pm
    • one
      @avp - I myself recently learned that, you can use the so-called Combining Code Points , and in this case we get 2 Unicode code point'a per visible symbol, or Precomposed Characters, can be used Precomposed Characters, where, roughly speaking, Precomposed Characters, embedded in character. - So, it is clear that in the first case, UTF-32 will not save. - Costantino Rupert
    • 2
      @avp As I understand it, UTF and in general all this Unicode standards are well described by this picture :) - Costantino Rupert
      private void button5_Click(object sender, EventArgs e) { int n = textBox5.Text.Length; char[] mas = new char[n]; for (int i=0; i<n; i++) { mas[i] = textBox5.Text[i]; } for (int i = n; i != 0; i--) { label4.Text += mas[i-1]; } } 

    This code will turn any string.

    • one
      Compare your level of response with the answers given earlier. IMHO, I would demolish your answer in your place. - Alexander Muksimov