title: Remove white spaces in string without split function
body: <p>Need to remove all excess white spaces in a string, including ones at the beginning and end. I cannot use the split function. Only if and while statements. I have this so far, but every time i run it, it only returns the input the exact same way.</p>
  
  <pre><code>def cleanstring(S):
    i=0
    startpos=0
    endpos=0
    end=-1
    word=0
  
  
    #find position of first letter
    while S[i]==(" "):
      i=i+0
      startpos=i
  
    #find last letter
    while (S[end]==(" ")):
       end=end-1
       endpos=S[len(S)-end]
  
    #make first letter found the first letter in the string
    if S[i]!=(" "):
      word=S[i]
  
    #start between startpos and endpos to find word
    while (i&lt;endpos) and (i&gt;startpos):
      while S[i]!=(" "):
        word=word+S[i]
      if S[i]==(" "):
        if (S[i+1]==("")) or (S[i-1]==(" ")):
          word=word+(" ")
        else:
          word=word+(" ")
    #return the word
   print(word)
  
  Input=["   Hello to   the world    "]  
  </code></pre>