Reversing a string using an array method (not doing the obvious Array.Reverse); method, it is more fun to do it with a loop!
Also incorporated the addition assignment operator (+=).
public class ReverseString { public string ReverseIt(string phrase) { string reversed = ""; string[] wordsArray = phrase.Split(' '); int length = wordsArray.Length; for (var i = length -1 ; i >= 0; i--) { reversed += wordsArray[i] + " "; } Console.WriteLine(reversed); return reversed; } }
class Program { static void Main(string[] args) { ReverseString rev = new ReverseString(); rev.ReverseIt("I have six locks on my door all in a row. When I go out, I lock every other one. I figure no matter how long somebody stands there picking the locks, they are always locking three."); Console.ReadLine(); } }