title: Copying characters from a source string to a buffer - C
body: <p>I am currently working on a computer science project and have gotten it totally working bar a very specific case which I cannot seem to fix. The goal of the code is to write a function int tokenCopy(char* dest, const char* src, int destSize) which copies characters from the given source string src into the given destination buffer dest, which is of size destSize, until either:</p>
  
  <p>the end of the source string occurs, or
  the destination buffer is full (allowing for the terminator that will be needed), or a space character is found in the input string</p>
  
  <p>whichever comes first. If the copying finishes because a space is found, the space is not copied. The destination string must always be correctly terminated. If there is insufficient space in the destination for the source string, the destination string must be a correctly terminated leading substring of the source string.</p>
  
  <p>The return value is the number of characters copied, not including the terminating null byte.</p>
  
  <p>Where testing in done in the form of:</p>
  
  <pre><code>char buff[3];
  int n = tokenCopy(buff, "This is a string", 3);
  printf("%d '%s'\n", n, buff);
  </code></pre>
  
  <p>This is my code so far:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
   int tokenCopy(char* dest, const char* src, int destSize){
          int new_value = 0;
          int i=0;
  
  
          for (;*src != ' '; new_value++){
          destSize = 1;
          return new_value;
          }
  
          for (; *src != '\0' &amp;&amp; *src!= ' '; new_value++)
          {
              while (i &lt; destSize-1 &amp;&amp; *src!= '\0'){
              *dest = *src;
              dest++;
              src++;
              new_value++;
              i++;
  
          *dest = '\0';
              }
          return new_value;
         }
      return new_value;
     }
  </code></pre>
  
  <p>However in the case of the following test:</p>
  
  <pre><code>char buff[10];
  int n = tokenCopy(buff, " ", 10);
  printf("%d '%s'\n", n, buff);
  </code></pre>
  
  <p>The output should be:</p>
  
  <pre><code>0 ''
  </code></pre>
  
  <p>However my code is outputting the zero, followed by the speech marks containing a different letter followed a question mark in a diamond per each change I try to make to the code. For reference I am using linux with the Geany IDE.</p>
  
  <p>Any help with this problem would be much appreciated.</p>