title: how do I split a string into array of string?
body: <p>For example:</p>
  
  <p>input(string): <code>foo $$ foo ## foo []</code></p>
  
  <p>search(string): <code>foo</code></p>
  
  <p>output(array): <code>$$ ,## ,[]</code></p>
  
  <p>I tried it:</p>
  
  <pre><code>char * str = "foo $$ foo ## foo []";
      char * s = "foo";
  
      int buf_len = 0;
      int len = strlen(s);
      int i = 0;
  
      char ** buffer = malloc(MAX_BUFFER_SIZE);
      char * tmpbuf = malloc(MAX_BUFFER_SIZE);
      char * p = str;
      char ** buf = buffer;
      char * tbuf = tmpbuf;
  
      while(*p)
      {
          if(*p == *s) 
          {
              while(*p == *(s + i)) 
              { 
                  i++;
                  p++;
              }
  
              if(i == len) 
              {
                  *buf ++ = tbuf;
                  memset(tbuf,0,buf_len);
                  i = buf_len = 0;
              }
          }
          else 
          {
              *tbuf ++= *p;
              buf_len ++;
          }
  
          p++;
      }
  
      *buf ++= NULL;
  
      int x;
      for(x = 0; buffer[x]; x++)
      {
          printf("%s\n", buffer[x]);
      }
  
      free(buffer);
      free(tmpbuf);
  </code></pre>
  
  <p>that show the following output:</p>
  
  <pre><code>$$ ## []
  ## []
  []
  </code></pre>
  
  <p>but the expected is:</p>
  
  <pre><code>$$  
  ##  
  []
  </code></pre>
  
  <p>how to fix this? </p>