title: How to restore string after using strtok()
body: <p>I have a project in which I need to sort multiple lines of text based on the second, third, etc word in each line, not the first word. For example,</p>
  
  <pre><code>this line is first
  
  but this line is second
  
  finally there is this line
  </code></pre>
  
  <p>and you choose to sort by the second word, it would turn into </p>
  
  <pre><code>this line is first
  
  finally there is this line
  
  but this line is second
  </code></pre>
  
  <p>(since line is before there is before this)</p>
  
  <p>I have a pointer to a char array that contains each line. So far what I've done is use strtok() to split each line up to the second word, but that changes the entire string to just that word and stores it in my array.  My code for the tokenize bit looks like this:</p>
  
  <pre><code> for (i = 0; i &lt; numLines; i++) {
     char* token = strtok(labels[i], " ");
     token = strtok(NULL, " ");
     labels[i] = token;
   }
  </code></pre>
  
  <p>This would give me the second word in each line, since I called strtok twice. Then I sort those words. (line, this, there) However, I need to put the string back together in it's original form. I'm aware that strtok turns the tokens into '\0', but Ive yet to find a way to get the original string back.</p>
  
  <p>I'm sure the answer lies in using pointers, but I'm confused what exactly I need to do next.</p>
  
  <p>I should mention I'm reading in the lines from an input file as shown:</p>
  
  <pre><code>for (i = 0; i &lt; numLines &amp;&amp; fgets(buffer, sizeof(buffer), fp) != 0; i++) {
    labels[i] = strdup(buffer);
  </code></pre>
  
  <p>Edit: my find_offset method</p>
  
  <pre><code>size_t find_offset(const char *s, int n) {
    size_t len;
    while (n &gt; 0) {
       len = strspn(s, " ");
       s += len;
    }
  
    return len;
  } 
  </code></pre>
  
  <p>Edit 2: The relevant code used to sort</p>
  
  <pre><code>//Getting the line and offset
  for (i = 0; i &lt; numLines &amp;&amp; fgets(buffer, sizeof(buffer), fp) != 0; i++) {
     labels[i].line = strdup(buffer);
     labels[i].offset = find_offset(labels[i].line, nth);
  }
  
  
  int n = sizeof(labels) / sizeof(labels[0]);
  qsort(labels, n, sizeof(*labels), myCompare);
  for (i = 0; i &lt; numLines; i++)
    printf("%d: %s", i, labels[i].line); //Print the sorted lines
  
  
  int myCompare(const void* a, const void* b) { //Compare function
    xline *xlineA = (xline *)a;
    xline *xlineB = (xline *)b;
  
    return strcmp(xlineA-&gt;line + xlineA-&gt;offset, xlineB-&gt;line + xlineB-&gt;offset);
  }
  </code></pre>