title: Reverse words of a sentence without using String.Split in C#
body: <p>Recently I have been asked in a discussion to write an algorithm to implement reverse of words of a sentence (Not reverse of whole sentence) without using string operations such as Split/Replace/Reverse/Join except ToCharArray and Length. The below is what I could devise in 5min of time. Though the algorithm is working fine, it seems bit ugly style of implementation. Can some body help me by polishing the code.</p>
  
  <pre><code>string ReverseWords(string s)
  {
      string reverseString = string.Empty;
      string word = string.Empty;
  
      var chars = s.ToCharArray();
      List&lt;ArrayList&gt; words = new List&lt;ArrayList&gt;();
      ArrayList addedChars = new ArrayList();
      Char[] reversedChars = new Char[chars.Length];
      int i = 1;
      foreach (char c in chars)
      {
          if (c != ' ')
          {
              addedChars.Add(c);
          }
          else
          {
              words.Add(new ArrayList(addedChars));
              addedChars.Clear();
          }
          if (i == s.Length)
          {
              words.Add(new ArrayList(addedChars));
              addedChars.Clear();
          }
          i++;
      }
      foreach (ArrayList a in words)
      {
          for (int counter = a.Count - 1; counter &gt;= 0; counter--)
          {
              reverseString += a[counter];
          }
          if(reverseString.Length &lt; s.Length)
              reverseString += " ";
      }
      return reverseString;
  }
  </code></pre>