title: Why does strtok() not tokenize the string a certain way?
body: <p>I'm trying to tokenize a string using brackets [] as delimiters. I can tokenize a string exactly how I want it with one input, but it has an error other times. For example, I have a string with characters before the delimiter and it works fine, but if nothing is before the delimiter then I run into errors.</p>
  
  <p>This one gives me an error. The <code>token2</code> ends up being <code>NULL</code> and <code>token</code> is <code>"name]"</code>  with the bracket still on there.</p>
  
  <pre><code>char name[] = "[name]";
  char *token = strtok(name, "[");
  char *token2 = strtok(NULL, "]");
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>token = name]
  token2 = NULL
  </code></pre>
  
  <p>However, if I have the following, then it works just fine.</p>
  
  <pre><code>char line[] = "Hello [name]";
  char *tok = strtok(line, "[");
  char *tok2 = strtok(NULL, "]");
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>tok = Hello
  tok2 = name
  </code></pre>
  
  <p>I don't understand what I'm doing wrong when the input is simply something like <code>"[name]"</code>. I want just what's inside the brackets only.</p>
  
  <p>Edit:
  Thanks for the input, everyone. I found a solution to what I'm trying to do. Per @Ryan and @StoryTeller's advice, I first checked if the input began with <code>[</code> and delimited with <code>[]</code>. Here's what I tried and worked for the input:</p>
  
  <pre><code>char name[] = "[name]", *token = NULL, *token2 = NULL;
  
  if (name[0] == '[')
  {
      token = strtok(name, "[]");
  }
  else
  {
      token = strtok(name, "[");
      token2 = strtok(NULL, "]");
  }
  </code></pre>