Word Scramble

Write a program that will reverse the letters in each of a sequence of words while preserving the order of the words themselves.

Input

The input file will consist of several lines of several words. Words are contiguous stretches of printable characters delimited by white space.

Output

The output will consist of the same lines and words as the input file. However, the letters within each word must be reversed.

Sample Input

I love you.

You love me.

We’re a happy family.

Sample Output

I evol .uoy

uoY evol .em

er’eW a yppah .ylimaf

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication3

{

    class Program

    {

        static void Main(string[] args)

        {

            string input = "We’re a happy family.";

            string output = "";

            string tempStr = "";

            for (int i = 0; i < input.Length – 1; i++)

            {

                if (input[i] != ‘ ‘)

                {

                    tempStr = tempStr + input[i].ToString();

                }

                else

                {

                    output = output + ‘ ‘;

                }

                if (input[i] != ‘ ‘ && input[i + 1] == ‘ ‘)

                {

                    output = output + Reverse(tempStr);

                    tempStr = "";

                }

            }

            if (input[input.Length – 1] != ‘ ‘ && tempStr.Length != 0)

            {

                tempStr = tempStr + input[input.Length – 1].ToString();

                output = output + Reverse(tempStr);

            }

            else

            {

                output = output + input[input.Length – 1].ToString();

            }

            Console.WriteLine(output);

        }

        public static string Reverse(string str)

        {

            int i = 0;

            int j = str.Length – 1;

            char[] chArray = str.ToCharArray();

            while (i < j)

            {

                char tempChar = chArray[i];

                chArray[i] = chArray[j];

                chArray[j] = tempChar;

                i++;

                j–;

            }

            return new string(chArray);

        }

    }

}

 

er’eW a yppah .ylimaf
Press any key to continue . . .

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s