Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is strtok_r() function in C language?
The strtok_r() function is a thread-safe alternative to the strtok() function for tokenizing strings. The "_r" suffix stands for "re-entrant," meaning this function can be safely interrupted and resumed without losing its state. This makes it ideal for multi-threaded applications where multiple threads might need to tokenize strings simultaneously.
A re-entrant function can be interrupted during execution and safely resumed later. Because strtok_r() maintains its context through an external pointer, it avoids the static variable issues that make strtok() non-thread-safe.
Syntax
char *strtok_r(char *str, const char *delim, char **saveptr);
Parameters
- str − The string to be tokenized (NULL for subsequent calls)
- delim − String containing delimiter characters
- saveptr − Pointer to store the context between calls
Return Value
Returns a pointer to the next token, or NULL if no more tokens are found.
Example: Basic String Tokenization
The following example demonstrates how to use strtok_r() to split a sentence into individual words −
#include <stdio.h>
#include <string.h>
int main() {
char input_string[] = "Hello Tutorials Point";
char token_list[20][20];
char *context = NULL;
char *token = strtok_r(input_string, " ", &context);
int num_tokens = 0;
while (token != NULL) {
strcpy(token_list[num_tokens], token);
num_tokens++;
token = strtok_r(NULL, " ", &context);
}
printf("Token List:<br>");
for (int i = 0; i < num_tokens; i++) {
printf("%s<br>", token_list[i]);
}
return 0;
}
Token List: Hello Tutorials Point
Example: Multiple Delimiters
This example shows how to use multiple delimiters to parse a CSV-like string −
#include <stdio.h>
#include <string.h>
int main() {
char data[] = "apple,banana;orange:grape";
char *context = NULL;
char *token = strtok_r(data, ",;:", &context);
printf("Parsed fruits:<br>");
while (token != NULL) {
printf("- %s<br>", token);
token = strtok_r(NULL, ",;:", &context);
}
return 0;
}
Parsed fruits: - apple - banana - orange - grape
Key Differences from strtok()
| Feature | strtok() | strtok_r() |
|---|---|---|
| Thread Safety | No | Yes |
| Context Storage | Internal static variable | External pointer parameter |
| Re-entrant | No | Yes |
| Parameters | 2 parameters | 3 parameters |
Conclusion
The strtok_r() function provides a thread-safe way to tokenize strings by using an external context pointer. It's essential for multi-threaded applications where string parsing needs to be performed safely without interference between different execution contexts.
