Write a program in C# Sharp to reverse the order of the given words?

Category > CSHARP || Published on : Friday, July 9, 2021 || Views: 1218 || program in C# Sharp to reverse the order of the given words?


Here Pawan Kumar will explain how to program in C# Sharp to reverse the order of the given words?

using System;
using System.Text;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            string DummyString = "hello how are you today? from www.sourcecodehub.com";
            Console.WriteLine(DummyString);
            Console.WriteLine(ReverseWordOrder(DummyString));
        }

        internal static string ReverseWordOrder(string str)
        {
            int i;
            StringBuilder reverseSentence = new StringBuilder();
            int Start = str.Length - 1;
            int End = str.Length - 1;
            while (Start > 0)
            {
                if (str[Start] == ' ')
                {
                    i = Start + 1;
                    while (i <= End)
                    {
                        reverseSentence.Append(str[i]);
                        i++;
                    }
                    reverseSentence.Append(' ');
                    End = Start - 1;
                }
                Start--;
            }
            for (i = 0; i <= End; i++)
            {
                reverseSentence.Append(str[i]);
            }
            return reverseSentence.ToString();
        }
    }
}

 

Download Source Codes