strtok

Syntax

char* strtok(void *str, const char *delimiters)

Arguments

str
The string to tokenize. On the second and repeated calls, this should be set to NULL to continue using the internal tokenizing buffer to search for the next token.
delimiters
A string of characters to use as delimiters (eg space, comma, tab symbol) that will split the string into tokens.

Return

Returns a pointer to the internal string buffer pointing to the current token, or NULL if the end of the string has been reached with no more tokens found.

Description

Splits a string into string "tokens", split by the given delimiters. Each call to strtok returns a single token.

You can use this function to split a string into individual words by using the space character as a delimiter. You might also use it to split a line of a CSV (comma-separated-value) file by using the comma symbol as a delimiter.

Example

%fml

OnFilterStart: {
  char* strbuffer;
  char* token;
  strbuffer = malloc(1000);
  sprintf(strbuffer, "FM is awesome!");
  printf("%s", strbuffer);
  token = strtok(strbuffer, " ");
  while(token != NULL) {
    printf("%s", token);
    // Use NULL here, not strbuffer
    // otherwise infinite loop here!
    token = strtok(NULL, " ");
  }
  free(strbuffer);
  return true;
}

See Also

strncpy, strcpy