Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is strtok_r() function in C language?
strtok_r() function in C language
The strtok_r() function is similar to the strtok() function. The only key difference is that the _r, which is called as re-entrant function.
A re-entrant function is a function which can be interrupted during its execution. This type of function can be used to resume execution.
Because of this fact, re-entrant functions are thread-safe, means they can safely be interrupted by threads without any harm.
strtok_r() function has an extra parameter called the context. so that function can resume at the right place.
Syntax
The syntax for strtok_r() function is as follows:
// header file to be included #include <string.h> // Function prototype char *strtok_r(char *string, const char *limiter, char **context);
Example of strtok_r() function
Following is the C program for the use of strtok_r() function −
#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);
// Index to token list. We will append to the list
int num_tokens = 0;
while (token != NULL) {
strcpy(token_list[num_tokens], token); // Copy to token list
num_tokens++;
token = strtok_r(NULL, " ", & context);
}
// Print the list of tokens
printf("Token List:
");
for (int i = 0; i < num_tokens; i++) {
printf("%s
", token_list[i]);
}
return 0;
}
Output
When the above program is executed, it produces the following result −
Token List: Hello Tutorials Point
Advertisements