Home » How to Reverse words from the string in C sharp

How to Reverse words from the string in C sharp

In this post you will get to know , how we can reverse the words present in the string,

Input: “Hello world” Output: “world Hello”

class Program
{
    static void Main(string[] args)
    {
        string inputS = "Hello world";
        string[] arr = inputS.Split(' ');
        int length = arr.Length - 1;
        string finalString = string.Empty;
        while (length > -1)
        {
            finalString = finalString +" "+ arr[length];
            length--;
        }
        Console.WriteLine(finalString);
        Console.ReadLine();
    }
}