C strtok function splits a string by a token

Syntax

C strtok function has the following syntax.

char *tokens = strtok(char *str1, char *splitter );

C strtok function is from header file string.h.

Description

C strtok function splits string by tokens.

Example

Use C strtok function to split a string by token.


#include <stdio.h>
#include <string.h>
/*from   w  ww.  j  a  v a 2s.  c o  m*/
int main()
{ 
   char string[] = "asd fasd fasdf asdfads fasdf";
   char *tokenPtr;
   
   printf( "%s\n%s\n\n%s\n",
      "The string to be tokenized is:", string, 
      "The tokens are:" );
          
   tokenPtr = strtok( string, " " ); 

   while ( tokenPtr != NULL ) { 
      printf( "%s\n", tokenPtr );
      tokenPtr = strtok( NULL, " " );
   }

   return 0;

}

The code above generates the following result.





















Home »
  C Language »
    Function Reference »




assert.h
ctype.h
math.h
setjmp.h
signal.h
stdio.h
stdlib.h
string.h
time.h
wctype.h