title: Removing contiguous spaces in a String without trim() and replaceAll()
body: <p>I have to remove leading and trailing spaces from the given string as well as combine the contiguous spaces. For example,</p>
  
  <pre><code>String str = "    this is    a   string  containing numerous  whitespaces   ";
  </code></pre>
  
  <p>and I need to return it as:</p>
  
  <pre><code>"this is a string containing numerous whitespaces";
  </code></pre>
  
  <p>But the problem is I can't use <code>String#trim()</code>. (This is a homework and I'm not allowed to use such methods.) I'm currently trying it by accessing each character one-by-one but quite unsuccessful.</p>
  
  <p>I need an optimized code for this. Could anybody help? I need it to be done by today :(</p>