1

input:string arabicStr = "inrr أربعة عشر anp خمسين paisle";

but my desire output is: "paisle. خمسين anp عشر أربعة inrr "

  • simply reverse the string on the basis of word. – Sheebu Ansari Oct 05 '18 at 09:48
  • See also [how to reverse a string in C#](https://stackoverflow.com/questions/228038/best-way-to-reverse-a-string). – Tetsuya Yamamoto Oct 05 '18 at 09:48
  • output to where? – Renatas M. Oct 05 '18 at 09:49
  • 1
    Check [Bidirectional Features in WPF Overview](https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/bidirectional-features-in-wpf-overview). You can change the `FlowDirection` attribute of an element or an entire Window – Panagiotis Kanavos Oct 05 '18 at 09:52
  • Your output is *not* the reverse of the input anyway. You reversed the order of the *words* only, without taking RTL into account at all. You can do that with `String.Join(" ", input.Split(' ').Reverse())`. What is the real question here? – Panagiotis Kanavos Oct 05 '18 at 09:57
  • i want to know is there any method for change the orientation of the string. becoz if string has english word then it display it from left to right once any word replace with arabic then i want previous string must and should display from right to left. – Sheebu Ansari Oct 05 '18 at 10:01
  • @SheebuAnsari as I said, you aren't changing the orientation, you aren't handling Arabic at all and in the end, WPF can work with RTL without modifying the string. Have you tried that? Besides, Unicode may contain RLM and LRM markers already. Do you have a **specific** problem? – Panagiotis Kanavos Oct 05 '18 at 10:02
  • Split, reverse and join: `string[] words=input.Split(); Array.Reverse(words); string result = string.Join(" ",words);` – Tim Schmelter Oct 05 '18 at 10:15

2 Answers2

1
class ReverseString
    {
        static void Main(string[] args)
        {
            int i;
            string Temp = string.Empty;
            string Str;
            Console.WriteLine("Enter string");
            Str = Console.ReadLine();
            int Prev = Str.Length - 1;
            for (i = Str.Length - 1; i >= 0; i--)
            {
                if (Str[i] == ' ' || i == 0)
                {
                    if (i == 0)
                        Temp += Str[i];
                    for (int j = i + 1; j <= Prev; j++)
                    {
                        Temp += Str[j];
                    }

                    Temp += ' ';
                    Prev = i - 1;
                }
                else
                {
                    continue;
                }
            }
            Console.WriteLine(Temp);
        }
    }
}
  • Why would anyone need this? It doesn't answer the question in the first place. .NET itself understands Unicode *and the flow marks inside it* like the [Right-To-Left](https://en.wikipedia.org/wiki/Right-to-left_mark) and Left-To-Right mark. This code doesn't. It can *easily* result in mangled strings – Panagiotis Kanavos Oct 05 '18 at 09:52
  • If you wanted to answer your own question, this isn't a good answer. It should be deleted. The question itself should be improved to explain what you actually want. You *DON'T* need to modify strings just to display them – Panagiotis Kanavos Oct 05 '18 at 09:54
  • If you wanted to reverse words without taking RTL into account, you could use `String.Join(" ", input.Split(' ').Reverse())`. What you posted here isn't a good solution because it generates a *LOT* of temporary strings. Strings are immutable so *any* string manipulation operation creates new strings. `Temp += ' ';` will create a *new* string, not append a character to the existing one – Panagiotis Kanavos Oct 05 '18 at 09:59
1

Just split it with Spaces, Reverse it and then again join it with Spaces:

string result = string.Join(" ", input.Split(' ').Reverse());
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171