strtok - C string.h

C examples for string.h:strtok

Type

function

From

<cstring> 
<string.h>

Description

Split string into tokens

Prototype

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

Parameters

Parameter Description
str C string to be toknized.
delimiters C string containing the delimiter characters.

Return Value

If a token is found, a pointer to the beginning of the token.

Otherwise, a null pointer. A null pointer is always returned when the end of the string is reached.

Demo Code


#include <stdio.h>
#include <string.h>

int main ()//w ww.  j a  v  a2 s . c o  m
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
  }
  return 0;
}

Related Tutorials