title: how to get substring in c with strchr
body: <p>I'm trying to fetch part of a string.<br>
  I have the following code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  char mystring[]="The quick brown fox jumps over the lazy dog";
  char word1[]="The";
  char * posb,pose;
  char * word2;
  int b,e,n;
  
  n=memcmp(mystring, word1, sizeof(word1)-1);
  if (n==0) printf("the first word is found!\n");
  posb=strchr(mystring,' ');   // posb will be a pointer to the first character
  b=posb-mystring+1;
  printf("posb -&gt; %d\n",posb);
  printf("b -&gt; %d\n",b);
  pose=strchr(mystring+b+1,' ');   // pose will be a pointer to the second character
  printf("calc e\n");
  e=pose-sizeof(mystring)+1;
  printf("pose -&gt; %s\n",pose);
  printf("e -&gt; %d\n",e);
  word2 = (char*) malloc (sizeof(char)*(e-b));
  memcpy(word2, posb, sizeof(word2));
  printf("%s:%i:%i:%i\n",word2, b, e, e-b);
  free (word2);
  </code></pre>
  
  <p>The problem is getting the second word and store it in word2. For this I try using <code>strchr</code> to locate the spaces. But the second time I use <code>strchr</code> I need an offset to find the second space. I tried the following:</p>
  
  <pre><code>pose=strchr(mystring+b+1,' ');
  pose=strchr(&amp;mystring[b+1],' ');
  </code></pre>
  
  <p>The variables <code>b</code> and <code>e</code> should contain the positions of the space character in <code>mystring</code>. <code>word2</code> should contain <code>quick</code> eventually.<br>
  Another solution would be to 'walk through' the string using a loop, but that would be cheating the <code>strchr</code> function.</p>