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.
// Splits str according to given delimiters. // and returns 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);
Geeks for Geeks
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() :
Geeks for Geeks
Another Example of strtok :
Geeks for geeks Contribute
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 delimeters.
1997, Ford, E350, ac, 3000.00,
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 contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- isalpha() and isdigit() functions in C with cstring examples.
- Commonly used String functions in C/C++ with Examples
- Methods vs. Functions in C++ with Examples
- fill() and fill_n() functions in C++ STL
- Forward List in C++ | Set 1 (Introduction and Important Functions)
- strdup() and strndup() functions in C/C++
- Binary Search functions in C++ STL (binary_search, lower_bound and upper_bound)
- Ceil and Floor functions in C++
- Pure Virtual Functions and Abstract Classes in C++
- Wide char and library functions in C++
- SQL general functions | NVL, NVL2, DECODE, COALESCE, NULLIF, LNNVL and NANVL
- Higher Order Functions and Currying
- Explicitly Defaulted and Deleted Functions in C++ 11
- beta(), betaf() and betal() functions in C++ STL
- std::legendre, std::legendref and std::legendrel functions in C++17
- Difference between input() and raw_input() functions in Python
- Virtual Functions and Runtime Polymorphism in C++ | Set 1 (Introduction)
- asin() and atan() functions in C/C++ with Example
- Python program to count upper and lower case characters without using inbuilt functions
Improved By : argone