NPSF3000

ReverseWordsInString

Jan 1st, 2012
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.65 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace ReverseWordsInString
  5. {
  6.     class Program
  7.     {
  8.         //  Task is to reverse the words in a string, as proposed by JustinLloyd
  9.         //  http://forum.unity3d.com/threads/117543-I-need-peer-reviewing-to-evaluate-my-abilities-as-a-programmer.?p=785796&viewfull=1#post785796
  10.         //  My understanding is that the key pricinciple is getting the job done in a developer efficent way, hardware be damned.
  11.  
  12.         //  This correlates with my experiments, that show much 'optimisation' is irrelevant, as can be seen in my pastebin submissions such as:
  13.         //  Culling System where I opted for brute force: http://pastebin.com/sf6Gf5cM which was sufficiently powerful
  14.  
  15.         static void Main(string[] args)
  16.         {
  17.             var sampleData = "the cat sat on the mat";  //It is a given that the string will be 'clean' as per this sample.
  18.  
  19.             Console.WriteLine("Solution One: {0}", SolutionOne(sampleData));
  20.             Console.WriteLine();
  21.             Console.WriteLine("Solution Two: {0}", SolutionTwo(sampleData));
  22.  
  23.             Console.ReadLine();
  24.         }
  25.  
  26.         //Solution one assumes the obvious - reverse the order of the words in the sentance.
  27.         static string SolutionOne(string data)
  28.         {
  29.             return string.Join(" ", data.Split(' ').Reverse());
  30.         }
  31.  
  32.         //Solution two assumes an alternate interpretation, reorder the letters in the words, not the order of the words themselves.
  33.         static string SolutionTwo(string data)
  34.         {
  35.             return string.Join(" ", data.Split(' ').Select(x => new string(x.Reverse().ToArray())));
  36.         }
  37.     }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment