Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

strtok - Split a string into tokens

&pagelevel(4)&pagelevel

Definition

#include <string.h>

char *strtok(char *s1, const char *s2);

strtok can be used to split a complete string s into substrings called “tokens”, e.g. a sentence into individual words, or a source program statement into its smallest syntactical units.
The start and end criterion for each token are separator characters (delimiters), which you specify in a second string s2. Tokens may be delimited by one or more such delimiters or by the beginning and end of the entire string s1. Blanks, colons, commas, etc. are typical delimiters between the words of a sentence. A different delimiter sequence s2 may be specified for each call or token.

strtok processes exactly one token per call. The first call returns a pointer to the beginning of the first token found. Each subsequent call returns a pointer to the beginning of the next token. The strtok function terminates each token with the null byte (\0).

To ensure that strtok processes the entire string s1 in succession, the start address, i.e. a pointer to s1, must only be passed in the first call. In all subsequent calls, s1 must be passed as a NULL pointer.

Return val.

Pointer to the beginning of a token.


At the first call, a pointer to the first token; at the next call, a pointer to the following token, etc. strtok terminates each token in s1 with a null byte (\0), each time overwriting the first delimiter it finds with \0.

NULL pointer

if no token, or no further token was found.

Example

#include <string.h>
#include <stdio.h>
int main(void)
{
  static char str[] = "?a???b,,,#c";
  char *t;;
  t = strtok(str, "?");        /* t points to the token "a" */
  t = strtok(NULL, ",");       /* t points to the token "??b" */
  t = strtok(NULL, "#,");      /* t points to the token "c" */
  t = strtok(NULL, "?");       /* t is a NULL pointer */
  return 0;
}