Sunday, January 30, 2022

Given a string, how do we reverse the order or words in the string. e.g. Input: Tomorrow is Sunday”. Output: Sunday is Tomorrow.

 Given a string, how do we reverse the order or words in the string. e.g. Input: Tomorrow is Sunday”. Output: Sunday is Tomorrow. 

I am using Visual Studio and open Console Application:

Add one class with named "ReverseString". Flowing code snips will help you.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace PracticTest

{

    class ReverseString

    { 

        public static String reverse(string authors)

        {                   

            string[] authorsList = authors.Split(' ');

            String result = "";

            for (int i = authorsList.Length - 1; i >= 0; i--)

            {

                result = result + authorsList[i] + " ";

            }

            return result;

        }

        public static String reverseWord(string authors)

        {

            string[] authorsList = authors.Split(' ');

            String result = "";

            for (int i = authorsList.Length - 1; i >= 0; i--)

            {

                string resultSub = string.Empty;

                string subString = authorsList[i];

                for (int j= subString.Length-1; j>=0;j--)

                {

                    resultSub = resultSub + subString[j];

                }

                result = result + resultSub+ " ";

            }

            return result;

        }

        public static String reverseWordOnly(string authors)

        {

            string[] authorsList = authors.Split(' ');

            String result = "";

            for (int i = 0; i <= authorsList.Length - 1; i++)

            {

                string resultSub = string.Empty;

                string subString = authorsList[i];

                for (int j = subString.Length - 1; j >= 0; j--)

                {

                    resultSub = resultSub + subString[j];

                }

                result = result + resultSub + " ";

            }

            return result;

        }

    }

}

---------------------------------------------------------------------------------------------------------

Call in Main method:

class Program
    {
        static void Main(string[] args)
        {           
            Console.WriteLine("Enter your details");
            string str = Console.ReadLine();
            string result = ReverseString.reverse(str);
            Console.WriteLine(result);
            result = ReverseString.reverseWord(str);
            Console.WriteLine(result);
            result = ReverseString.reverseWordOnly(str);
            Console.WriteLine(result);
            Console.ReadLine();
        }
    }


Output:





No comments:

Post a Comment

Lab 09: Publish and subscribe to Event Grid events

  Microsoft Azure user interface Given the dynamic nature of Microsoft cloud tools, you might experience Azure UI changes that occur after t...