title: Why not all non-space and not * symbols copied? Why strcpy modifies source?
body: <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  int main(void){
      char q[100*1000+2];
      fgets(q,100*1000+2,stdin);//enter string
      int len=strlen(q)-1;
      char s[len];
      int i,j;
      for(i=j=0;q[i]!='\0';i++)
          if(q[i]!=' '&amp;&amp;q[i]!='\n'&amp;&amp;q[i]!='*')
          {s[j]=q[i];j++;}//remove all spaces, \n's and *
      len=j;//new length is length of s
      s[j]='\0';
      char copy[len];//copy s
      printf(&quot;s=%s\n&quot;,s);
      strcpy(copy,s);
      printf(&quot;s=%s\n&quot;,s);
      return 0;
  }
  </code></pre>
  <p>For <code>x^125-228-7x^27</code> (0) program outputs as it should:</p>
  <blockquote>
  <p>s=x^125-228-7x^27<br />
  s=x^125-228-7x^27</p>
  </blockquote>
  <p>Then input is <code>+x^125-228-7x^274</code>(1):</p>
  <blockquote>
  <p>s=+x^125-228-7x^27<br />
  s=+x^125-228-7x^27</p>
  </blockquote>
  <p>But desired output is:</p>
  <blockquote>
  <p>s=+x^125-228-7x^274<br />
  s=+x^125-228-7x^274</p>
  </blockquote>
  <p>Or <code>+x^125-228-7x^27</code>(2):</p>
  <blockquote>
  <p>s=+x^125-228-7x^27<br />
  s=</p>
  </blockquote>
  <p>Even it should output:</p>
  <blockquote>
  <p>s=+x^125-228-7x^27<br />
  s=+x^125-228-7x^27</p>
  </blockquote>
  <p>If I replace <code>strcpy(copy,s)</code> to <code>memcpy(copy,s,len)</code> then (2) outputs</p>
  <blockquote>
  <p>s=+x^125-228-7x^27<br />
  s=+x^125-228-7x^27</p>
  </blockquote>
  <p>But with (1) it is still the same.</p>