Reverse the Words in a Given String

This simple C# program reverses the words that reside in a string. The words are assumed to be separated by a single space character. The space character along with other consecutive letters forms a single word. Check the program below for details:




using System;

namespace Reverse_String_Test
{
    class Program
    {
        public static string reverseIt(string strSource)
        {
            string[] arySource = strSource.Split(new char[] { ' ' });
            string strReverse = string.Empty;
           
            for (int i = arySource.Length - 1; i >= 0; i--)
            {
                strReverse = strReverse + " " + arySource[i];
            }
           
            Console.WriteLine("Original String: \n\n" + strSource);
           
            Console.WriteLine("\n\nReversed String: \n\n" + strReverse);
           
            return strReverse;
        }

        static void Main(string[] args)
        {
            reverseIt(" I Am In Love With www.code-kings.blogspot.com.com");

            Console.WriteLine("\nPress any key to continue....");
            Console.ReadKey();
        }
    }

}
Custom Search