The strtok () function in C

Char * strtok function (char * str, const char * delim) divides the string str into a sequence of tokens separately separated by delim separators (eg comma, ...).

Char * strtok function (char * str, const char * delim) divides the string str into a sequence of tokens separately separated by delim separators (eg comma, .).

Declare strtok () function in C

Below is the declaration for strtok () in C:

 char * strtok ( char * str , const char * delim ) 

Parameters

str - The content of this string is modified and divided into smaller strings (tokens).

delim - This is the string containing Delimiter (only separators). They can be varied depending on the call.

Returns the value

This function returns the pointer to the last token found in the string. A null pointer is returned if no token is obtained.

For example

The following C program illustrates the usage of strtok () in C:

 #include #include int main () { char str [ 80 ] = "Hoc C - co ban va nang cao - tai QTM" ; const char s [ 2 ] = "-" ; char * token ; /* lay token dau tien */ token = strtok ( str , s ); /* duyet qua cac token con lai */ while ( token != NULL ) { printf ( " %sn" , token ); token = strtok ( NULL , s ); } return ( 0 ); } 

Compile and run C program and see the results.

According to Tutorialspoint

Previous article: Function strstr () in C

Next article: Ham strxfrm () in C

5 ★ | 1 Vote