Now I could have used the built in fuction of .reverse(), but then how would that show off my skillsets. ;)
Execute here: https://dotnetfiddle.net/FV8fFv
using System; using System.Text; using System.Collections.Generic; public class Program { public static void Main() { var str = "Hello World!"; var arr = str.ToCharArray(); //Reset Data NormalWay(arr); arr = str.ToCharArray(); //Reset Data ForLoop(arr); arr = str.ToCharArray(); //Reset Data ForEachStack(arr); } private static void NormalWay(char[] data){ Console.WriteLine("\nNormal Way."); Array.Reverse(data); Console.WriteLine(data); } private static void ForLoop(char[] data){ Console.WriteLine("\nUsing String Builder and a for loop."); var sb = new StringBuilder(); for(var i = data.Length - 1 ; i >= 0 ; i--) { sb.Append(data[i]); } Console.WriteLine(sb.ToString()); } private static void ForEachStack(char[] data){ Console.WriteLine("\nUsing a stack and a foreach loop."); var stk = new Stack<char>(); foreach(var n in data) stk.Push(n); Console.WriteLine(stk.ToArray()); } }
Reblogged this on Dinesh Ram Kali..