Interview Question

Software Development Engineer Intern Interview Seattle, WA

Reveres all the words in a string before you encounter a

  word which has 'x' in it. Hi, I like Microsoft asdfxab Settle -> Microsoft like I Hi, asdfxab seattle.
Answer

Interview Answer

1 Answer

1

using System;
using System.Linq;

namespace ReverseWords
{
    class Program
    {
        static void Main()
        {
            const string inputStr = "Hi, I like Microsoft asdfxab Settle";

            string reversedStr = String.Join(" ", inputStr.Split(' ').TakeWhile(n => !n.Contains("x")).Reverse());
            string resultStr = String.Concat(reversedStr, inputStr.Substring(reversedStr.Length));

            Console.WriteLine(resultStr);
        }
    }
}

andrejev2006 on Feb 13, 2012

Add Answers or Comments

To comment on this question, Sign In with Facebook or Sign Up