What is the strtok Function?
The method strtok divides the string stored in Str using specified delimiters and returns the next token each time it is called. It is necessary to call strtok multiple times to retrieve all tokens. Once all tokens have been extracted, strtok will return NULL.
Syntax of strtok:
It has the following syntax:
char *strtok(char *str, const char *delims);
Parameters:
It serves as the reference to the string that will undergo tokenization.
delims: It is a string containing all delimiters.
Return Value:
It provides the reference to the initial token found within the string.
It provides a NULL value in case no additional tokens are detected.
Program:
Let's consider an illustration for dividing a string utilizing the strtok function in the C programming language.
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "LOGIC PRACTICE";
char* s = strtok(str, " - ");
while (s != NULL) {
printf(" %s\n", s);
s= strtok(NULL, " - ");
}
return 0;
}
Output:
[Program Output]
What is strtok_r Function?
The Strtokr function carries out the identical task of breaking down a string into a series of tokens like the strtok function in the C language. Acting as a reentrant version of strtok, strtokr ensures thread safety.
Syntax of strtok_r:
It has the following syntax:
char *strtok_r(char *str, const char *delim, char **saveptr);
Parameters:
It serves as the reference to the string that will undergo tokenization.
delims: It is a string containing all delimiters.
saveptr: It represents a pointer to a char * variable which serves as an internal mechanism for strtok_r to retain context across consecutive invocations that analyze the identical string.
Return Value:
It provides the reference to the initial token found within the string.
It returns NULL when no additional tokens are discovered.
Program:
Let's consider an instance where we split a string utilizing the strtok_r function in the C programming language.
// C program to demonstrate the working of strtok_r()
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "LOGIC PRACTICE";
char* s;
char* r = str;
while ((token = strtok_r(r, " ", &r)))
printf("%s\n", s);
return (0);
}
Output:
[Program Output]
Difference Between strtok and strtok_r
There exist various distinctions between the strtok and strtokr functions in C. Here are some key variances between the strtok and strtokr functions in C:
| S.no | strtok() | strtok_r() |
|---|---|---|
1. |
It is used to break stringstrinto a series of tokens. | It is used to decode a string into a pattern for tokens. |
2. |
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); |
3. |
It uses thedelimiterto proceed. | It is a re-entered variant ofstrtok(). |
4. |
It takes two parameters. | It takes three parameters. |
5. |
It returns a pointer to the first token found in the string. | It returns a pointer to the first token found in the string. |
6. |
It is not thread-safe. | It is thread-safe. |
Benefits of strtok_r
A string can be tokenized (divided) into smaller substrings using the strtokr method, which is a version of the strtok function in C and C++. Strtokr is thread-safe and reentrant, making it a safer option in multithreaded or reentrant applications. The main distinction between strtok and strtokr is that strtokr is thread-safe and reentrant. The advantages of using strtok_r are as follows:
- Thread Safety: Strtokr needs you to give a pointer to a saveptr parameter in contrast to strtok, which uses a static internal pointer to keep track of the current parsing position. As a result, strtokr can be called concurrently without interfering with other threads because each thread can have its saveptr, making strtok_r thread-safe.
- Reentrancy: Because strtok_r is reentrant, it can be called without risk from inside signal handlers or nested function calls without affecting how other strings are parsed. It increases its adaptability to complex program frameworks.
- Predictable Behavior: Compared to strtok function, which depends on a hidden static pointer, strtokr's behaviour is simpler to comprehend and forecast because the saveptr pointer is clearly provided. It can make it easier to debug and maintain code that utilizes strtokr function.
- Portability: Strtok_r is a standardized function specified in the POSIX standard (IEEE Std 1003.1), making it more portable across multiple platforms and systems. Strtok is widely available and supported in most C and C++ libraries.
- Custom Parsing: By repeatedly invoking strtok_r with various delimiters and the same saveptr, you can tokenize a text with numerous delimiters. When working with complex input data, this flexibility can come in helpful.'
Conclusion:
In summary, strtok_r serves as a safer and more adaptable alternative to strtok when parsing strings within a multithreaded or reentrant environment. The inclusion of an explicit saveptr parameter and its ability to handle multiple threads make it the optimal choice.