title: a programm which removes excess spaces from a string
body: <p>I wrote a program which is supposed to remove excess spaces from a string. But it only shows characters before spaces. It finds a space and checks the character after that whether it is a space. Depending on excess spaces it shifts other characters over excess spaces. But output is very confusing.</p>
  
  <p>input: "qwe(2 spaces)rt(one space)y"</p>
  
  <p>output: "qwe(one space)rt(one space)y"</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  int main(){
      string a;
      cin &gt;&gt; a;
      int len = a.length();
      int new_len=len;
      int z,s=0;
      for(int i=0; i&lt;new_len; i++){
          if(a[i]==' '){
              z=i+1;
              s=0;
              //Assigning the number of excess spaces to s.
              while(a[z]==' '){
                  s++;
                  z++;
              }
              //doing the shifting here.
              if(s&gt;0){
                  for(int l=i+1; l&lt;new_len-s; l++){
                      a[l]=a[s+l];
                  }
              }
              new_len-=s;
          }
  
      }
      cout &lt;&lt; a &lt;&lt; endl;
      cout &lt;&lt; a.length();
      system("pause");
      return 0;
  }
  </code></pre>