title: two C functions trying to return the first word in a string
body: <p>I have created the two following functions. The first, eatWrd, returns the first word in a string without any white spaces, and removes the first word from the input string: </p>
  
  <p>MAX is a number representing the max length of a string</p>
  
  <pre><code>char* eatWrd(char * cmd)
  {
    int i = 0;        //i will hold my place in cmd
    int count = 0;    //count will hold the position of the second word
    int fw = 0;       //fw will hold the position of the first word
    char rest[MAX]; // rest will hold cmd without the first word
    char word[MAX]; // word will hold the first word
  
    // start by removing initial white spaces
    while(cmd[i] == ' ' || cmd[i] == '\t'){
      i++;
      count++;
      fw++;
    }
  
    // now start reading the first word until white spaces or terminating characters
    while(cmd[i] != ' ' &amp;&amp; cmd[i] != '\t' &amp;&amp; cmd[i] != '\n' &amp;&amp; cmd[i] != '\0'){
      word[i-fw] = cmd[i];
      i++;
      count++;
    }
    word[i-fw] = '\0';
  
    // now continue past white spaces after the first word
    while(cmd[i] == ' ' || cmd[i] == '\t'){
      i++;
      count++;
    }
  
    // finally save the rest of cmd
    while(cmd[i] != '\n' &amp;&amp; cmd[i] != '\0'){
      rest[i-count] = cmd[i];
      i++;
    }
    rest[i-count] = '\0';
  
    // reset cmd, and copy rest back into it
    memset(cmd, 0, MAX);
    strcpy(cmd, rest);
  
    // return word as a char *
    char *ret = word;
    return ret;
  }
  </code></pre>
  
  <p>The second, frstWrd, just returns the first word without modifying the input string:</p>
  
  <pre><code>// this function is very similar to the first without modifying cmd
  char* frstWrd(char * cmd)
  {
    int i = 0;
    int fw = 0;
    char word[MAX];
  
    while(cmd[i] == ' ' || cmd[i] == '\t'){
      i++;
      fw++;
    }
  
    while(cmd[i] != ' ' &amp;&amp; cmd[i] != '\t' &amp;&amp; cmd[i] != '\n' &amp;&amp; cmd[i] != '\0'){
      word[i-fw] = cmd[i];
      i++;
    }
    word[i-fw] = '\0';
  
    char *ret = word;
    return ret;
  }
  </code></pre>
  
  <p>To test the function, I used fgets to read a string from the User(me), and then I printed three strings (frstWrd(input), eatWrd(input), eatWrd(input)). I would have expected that given a string, "my name is tim" for example, the program would print "my my name", but instead it prints the third word three times over, "is is is":</p>
  
  <pre><code>// now simply test the functions
  main()
  {
    char input[MAX];
    fgets(input, MAX - 1, stdin);
    printf("%s %s %s", frstWrd(input), eatWrd(input), eatWrd(input));
  }
  </code></pre>
  
  <p>I have looked over my functions over and over and cannot see the mistake. I believe there is simply something I don't know about printf, or about using multiple string modification functions as arguments in another function. Any insight would be helpful thanks.</p>