String splitting is the process of dividing a string into smaller parts based on a specified delimiter. In C, the strtok() and strtok_r() functions are commonly used to split strings into tokens.
strtok()splits a string by a delimiter and returns one token at a time.strtok_r()is a reentrant version ofstrtok()that is safer for multithreaded programs.
strtok() Function
The 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.
Syntax
char *strtok(char *str, const char *delims);
Parameters:
- str: It is the pointer to the string to be tokenized.
- delims: It is a string containing all delimiters.
Return Value:
- It returns the pointer to the first token encountered in the string.
- It returns NULL if there are no more tokens found.
Example: C Program to demonstrate how to split a string using strtok().
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Geeks-for-Geeks";
// Returns first token
char* token = strtok(str, " - ");
// Keep printing tokens while one of the
// delimiters present in str[].
while (token != NULL) {
printf(" % s\n", token);
token = strtok(NULL, " - ");
}
return 0;
}
Output
Geeks for Geeks
Example: Program to demonstrates the use of the strtok() function to tokenize a string based on a delimiter.
#include <stdio.h>
#include <string.h>
// Driver function
int main()
{
// Declaration of string
char gfg[100] = " Geeks - for - geeks - Contribute";
// Declaration of delimiter
const char s[4] = "-";
char* tok;
// Use of strtok
// get first token
tok = strtok(gfg, s);
// Checks for delimiter
while (tok != 0) {
printf(" %s\n", tok);
// Use of strtok
// go through other tokens
tok = strtok(0, s);
}
return (0);
}
Output
Geeks for geeks Contribute
strtok_r() Function
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(), hence it is thread safe.
Syntax
char *strtok_r(char *str, const char *delim, char **saveptr);
Parameters:
- str: It is the pointer to the string to be tokenized.
- delims: It is a string containing all delimiters.
- saveptr: It 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.
Return Value:
- It returns the pointer to the first token encountered in the string.
- It returns NULL if there are no more tokens found.
Example: a Simple C program to show the use of strtok_r().
#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
GeeksExample: The below C program demonstrates the use of strtok_r() function for Nested Tokenization.
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Hello, World! Geeks for Geeks.";
const char outer_delimiters[] = "!.";
const char inner_delimiters[] = " ,";
char* token;
char* outer_saveptr = NULL;
char* inner_saveptr = NULL;
token = strtok_r(str, outer_delimiters, &outer_saveptr);
while (token != NULL) {
printf("Outer Token: %s\n", token);
char* inner_token = strtok_r(
token, inner_delimiters, &inner_saveptr);
while (inner_token != NULL) {
printf("Inner Token: %s\n", inner_token);
inner_token = strtok_r(NULL, inner_delimiters,
&inner_saveptr);
}
token = strtok_r(NULL, outer_delimiters,
&outer_saveptr);
}
return 0;
}
Output
Outer Token: Hello, World
Inner Token: Hello
Inner Token: World
Outer Token: Geeks for Geeks
Inner Token: Geeks
Inner Token: for
Inner Token: GeeksDifference Between strtok() and strtok_r()
Let us see the differences between strtok() and strtok_r() functions in a tabular form as shown below:
| strtok() | strtok_r() |
|---|---|
| It is used to break a string into a series of tokens. | It is used to decode a string into a pattern for tokens. |
Syntax:char *strtok(char *str, const char *delim) | Syntax:char *strtok_r(char *string, const char *delimiter, char **context) |
| It uses the delimiter to proceed. | It is a reentrant version of strtok(). |
| It takes two parameters. | It takes three parameters. |
| It returns a pointer to the first token found in the string. | It returns a pointer to the first token found in the string. |
| It is not thread-safe. | It is thread-safe. |