For reversing the word in a string I used following algorithm

  1. Traverse the string and push the characters in stack, until we get space
  2. When space is encountered, pop the element from stack and enqueue in queue
  3. enqueue the spaces in queue
  4. once the traverse is complete add the last word in queue

Stack is used to reverse the characters and queue is used to maintain the order of characters

 

   public static void ReverseWordOfString(string s)
    {
      if (string.IsNullOrEmpty(s))
      {
        Console.WriteLine("String is empty");
        return;
      }

      Stack<char> charStack = new Stack<char>();
      var charArray = s.ToCharArray();
      int length = charArray.Length;
      Queue<char> result = new Queue<char>();
      Console.WriteLine("input string : {0}", s);
      for (int i = 0; i < length; i++)
      {
        if (charArray[i] != ' ')
        {
          charStack.Push(charArray[i]);
        }
        else
        {
          while (charStack.Count > 0)
          {
            result.Enqueue(charStack.Pop());
          }
          result.Enqueue(' ');
        }
      }
      while (charStack.Count > 0)
      {
        result.Enqueue(charStack.Pop());

      }

      Console.Write("String After reversing the words : ");
      Console.Write(result.ToArray());
      Console.WriteLine();

    }