strtok() and strtok_r() functions in C with examples – GeeksforGeeks

C provides two functions strtok() and strtok_r() for splitting a string by some delimiter. Splitting a string is a very common task. For example, we have a comma-separated list of items from a file and we want individual items in an array. 

strtok() Method:  Splits str[] according to given delimiters and returns the next token. It needs to be called in a loop to get all tokens. It returns NULL when there are no more tokens.

char * strtok(char str[], const char *delims);

Example:

Tóm Tắt

C




#include& lt; stdio.h & gt;

#include& lt; string.h & gt;

 

int main()

{

    char str[] = "Geeks-for-Geeks"

    ;

 

    

    char* token = strtok(str, " - ");

 

    

    

    while (token != NULL) {

        printf(" % s\n & quot;, token);

        token = strtok(NULL, " - ");

    }

 

    return 0;

}



Output: 

Geeks
for
Geeks

strtok_r(): Just like strtok() function in C, strtok_r() does the same task of parsing a string into a sequence of tokens. strtok_r() is a reentrant version of strtok(). There are two ways we can call strtok_r() 

// The third argument saveptr is a pointer to a char * 
// variable that is used internally by strtok_r() in 
// order to maintain context between successive calls
// that parse the same string.
char *strtok_r(char *str, const char *delim, char **saveptr);

Below is a simple C program to show the use of strtok_r() :
 

CPP




#include <stdio.h>

#include <string.h>

 

int main()

{

    char str[] = "Geeks for Geeks";

    char* token;

    char* rest = str;

 

    while ((token = strtok_r(rest, " ", &rest)))

        printf("%s\n", token);

 

    return (0);

}



Output: 

Geeks
for
Geeks

Example 2 

C




#include <stdio.h>

#include <string.h>

 

int main()

{

    

    char gfg[100] = " Geeks - for - geeks - Contribute";

 

    

    const char s[4] = "-";

    char* tok;

 

    

    

    tok = strtok(gfg, s);

 

    

    while (tok != 0) {

        printf(" %s\n", tok);

 

        

        

        tok = strtok(0, s);

    }

 

    return (0);

}



Output: 

Geeks
for
geeks
Contribute

Practical Application: strtok can be used to split a string in multiple strings based on some separators. A simple CSV file support might be implemented using this function. CSV files have commas as delimiters.

Example:

C




#include <stdio.h>

#include <string.h>

 

int main()

{

    

    

    char gfg[100] = " 1997 Ford E350 ac 3000.00";

 

    

    const char s[4] = " ";

    char* tok;

 

    

    

    tok = strtok(gfg, s);

 

    

    while (tok != 0) {

        printf("%s, ", tok);

 

        

        

        tok = strtok(0, s);

    }

 

    return (0);

}



Output: 

1997, Ford, E350, ac, 3000.00,

Let us see the differences in a tabular form as shown below as follows: 

strtok() strtok_r() It is used to break string str into a series of tokensIt is used to decode a string into a pattern for tokens.

The syntax is as follows:

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

Its syntax is as follows:
char *strtok_r(char *string, const char *limiter, char **context);It uses the delimiter to proceed.It is a re-entered variant of strtok().It takes two parameters.It takes three parameters.Its return value is a pointer to the first token found in the stringIt is defined in header file <string.h>

This article is contributed by MAZHAR IMAM KHAN and shantanu_23. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above. 

My Personal Notes

arrow_drop_up