Wednesday, August 3, 2011

Food for thought

I had this question on an interview where I was asked to solve this problem on C#. Reverse a string :) well my eyes were gleaming when i heard the question, best part being asking me to do it in all possible ways I could think off to answer it immediately. I was happy with the number of answers I could do it in that short tenure of time but  I realized how knowing a framework in detail could streamline the thought process very quickly. I am going to list my answer in this post so that someone might bump on it on the rare instance and find it useful.
class Reverse
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter Input String:");
            string stringToBeReversed = Console.ReadLine();
            string reversedString = ReverseTheString(stringToBeReversed);
            Console.WriteLine("Reversed String is: " + reversedString);
            Console.ReadLine();
        }
 
        private static string ReverseTheString(string stringToBeReversed)
        {
            //Nifty one ain't it?
   char[] chArray= stringToBeReversed.ToCharArray();
   Array.Reverse(chArray);
   return new String(chArray);
  
        }
   
   private static string ReverseTheString1(string stringToBeReversed)
        {
            // interviewers love one liners don't they?? :)
   
   return(new String(stringToBeReversed.Reverse().ToArray()));
  
        }
   
   private static string ReverseTheString2(string stringToBeReversed)
   {
            //Old fashioned way how could i ever miss the one with good'ol for loop
   string sOutput = String.Empty;
   char[] chArray = stringToBeReversed.ToCharArray();
   
   for (int i = chArray.Length - 1; i > -1; i--)
   {
   sOutput += chArray[i];
   } 
   return sOutput;
   }
  
  private static string ReverseTheString3(string stringToBeReversed)
        {
            //Old fashioned way with string Builder upgrade
            StringBuilder sOutput = new StringBuilder();
            char[] chArray = stringToBeReversed.ToCharArray();

            for (int i = chArray.Length - 1; i > -1; i--)
            {
                sOutput.Append(chArray[i]);
            }
            return sOutput.ToString(); ;

        }

  
  private static string ReverseTheString4(string stringToBeReversed)
   {
         //one with recursion
   if (stringToBeReversed.Length == 1)
   return stringToBeReversed;
   else
   return stringToBeReversed[stringToBeReversed.Length - 1] + ReverseTheString4(stringToBeReversed.Substring(0, stringToBeReversed.Length - 1));
  }
  
  private static string ReverseTheString5(string stringToBeReversed)
        {
            //one with LINQ :)
            var reversedValue = stringToBeReversed.ToCharArray()
                         .Select(ch => ch.ToString())
                         .Aggregate<string>((xs, x) => x + xs);

            return reversedValue.ToString();
        }
  
   
    }

It's a real simple piece of code I posted it here since I felt the urge to write something since it's been a while I posted something on here. I am currently working on a top secret venture currently and once I form a working prototype I would definitely post it here. Hope someone finds my little post amusing.

Until Next time!!

2 comments:

  1. //all one - line lovers need to move to python.
    s = 'Reverse'
    print s[::-1]

    ReplyDelete
    Replies
    1. Honestly I didn't get an email about your comment til now. Python peru kevalama iruku.

      Delete