C programming strtok string.h


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

In C programming the <string.h> strtok function isolates a token or sequence of character from s1 with s2 acting as the delimiter character.Note this simply means a sequence of character is extracted from the string s1 up to the character similar to any of the first character of s2 encountered in s1.The first character of s2 encountered in s1 is replaced by null and the sequence is returned.If any of the character of s2 is not found in s1 then the whole s1 string is returned.A code example is given below.

Code example

char s1[]=”string” , ret;

ret=strtok( s1 , “man”);
printf( “ret=%s” , ret);

ret=strtok( s1 , “v” );
printf( “\nret=%s” , ret);

Output,

ret=stri
ret=string

In the first ‘strtok’ call the first similar character of “man” string in s1 is ‘n’,so the sequence up to “stri” of s1 is returned,here the delimiter character ‘n’ is replaced with NULL.

In the second call ‘strtok’ call the character ‘v’ is not found in s1,so the whole string s1 is returned.

Note we can separate a sequence of characters also know as token,from s1 with each subsequent call to ‘strotk’ function,how it is done is explain below.





Subsequent call to strtok

While making a subsequent call to ‘strtok’ we must note certain things:

i) In the first ‘strotk’ call if the delimiter(the second argument) character is found in the string s1 then the character next to the delimiter will act as the beginning of the string in the second call with the the second argument as the delimiter character:provided by the programmer.In the second call if the delimiter character is found in the remaining sequence of s1 then the sequence up to that delimiter is returned and the character next to the delimiter will act as the beginning of the sequence for the subsequent call to ‘strtok’.Note in all the second and third and subsequent call the first argument passed is NULL.

Code example

char “Directory=C,visual_studio_program/CSTRING_HEADER,string_strtok;” , *s2 , *s3 , *s4;

s2=strtok( s1 , “=”); //first call

s3=strtok( NULL , “/”); //second call

s4=strtok( s1 , “;” ); //third call

printf(“s2=%s”, s2);

printf(“\ns3=%s”, s3);

printf(“\ns4=%s”, s4);

Output,

s2=Directory
s3=C,visual_studio_program
s4=CSTRING_HEADER,string_strtok

In the first call the delimiter ‘=’ is found just after the token “Directory” so the token “Directory” is returned.

In the second call the first argument passed is NULL and the delimiter is “/”,this delimiter will be searched for in the remaining sequence “C,visual_studio_program/CSTRING_HEADER,string_strtok;” and it occur just after the token “C,visual_studio_program” so the token “C,visual_studio_program” is returned.

In the third call,the first argument passed is NULL and the delimiter is ‘;’,this delimiter will be searched in the remaining sequence “CSTRING_HEADER,string_strtok;” and occur just after the token “CSTRING_HEADER,string_strtok” ,so the token “CSTRING_HEADER,string_strtok” is returned.

I am sure it is clear by now how the concept of subsequent call to ‘strtok’ can isolate different tokens each time it is called.


ii) In the first or second or the subsequent call if the delimiter is not found in the string then the next and the remaining call will always return null.

Code example

char s1[]=”Corecplusplustutorial!” , ret1 , ret2 , ret3;

ret1=strtok( s1 , “p”);

ret2=strtok( s1 , “v” );

ret3=strtok( s1 , “!” );

printf( “ret1=%s” , ret1);

printf( “\nret2=%s” , ret2);

printf( “\nre3=%s” , ret3);

Output in VS,

ret1=Corec
ret2=lusplustutorial!
ret3=(null)

In the second call since ‘v’ is not found the remaining sequence is returned.

In the third call the returned value is null,since in the second call the delimiter was not found in the sequence.


Related links

->C strcspn string.h

->C strspn string.h