We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Monday, June 15, 2015

Write a program reverse the words in a given string.

    class Program
    {
        static void Main(string[] args)
        {
            int i, j = 0;
            Console.WriteLine("Enter a string ");
            string s = Console.ReadLine();

            Console.WriteLine();
            for (i = 0; i < s.Length; i++)
            {

                if (s[i] == ' ')
                {
                    j++;
                }

            }

            string[] rev = new string[j + 1];
            int t = j;
            for (i = 0; i < s.Length; i++)
            {

                if (s[i] == ' ')
                    j--;

                else

                    rev[j] = rev[j] + s[i];

            }
            for (i = 0; i <= t; i++)
            {
                Console.Write(rev[i] + " ");
            }

            Console.WriteLine();

            Console.ReadLine();
        }


    }

Output:

No comments: