In the realm of string manipulation in C, the strtok_r function is a robust tool that allows programmers to dissect a string into tokens (or chunks) based on a specific delimiter. It is part of the ANSI C standard and is present in most C libraries. Let's explore this function with a strtok_r example to clarify its usage and benefits.
Before delving into the strtok_r example, let's briefly understand the differences between strtok and strtok_r. The main distinction lies in their handling of pointers. The strtok function modifies the original string, which might not always be desirable. In contrast, strtok_r does not alter the original string and preserves the position of the last read token, making it more versatile and safer to use.
Basic Syntax and Initialization of strtok_r
The basic syntax for strtok_r is:
char *strtok_r(char *str, const char *delim, char **saveptr);Here, str is the input string, delim is the delimiter used to parse the string, and saveptr is a pointer to a char*, used to maintain the position of the last read token.
To initialize strtok_r, you need to call it with a non-null value for the input string, and then call it again with a null value for the input string and the same saveptr. This initializes the saveptr and sets it ready for subsequent calls.
Initializing and Using strtok_r
Here's an example of initializing and using strtok_r: ```c char str[] = "lopez,John,Doe,jane,Doe"; char *token; char *saveptr; strtok_r(str, ",", &saveptr); token = strtok_r(NULL, ",", &saveptr); while (token != NULL) { printf("%s\n", token); token = strtok_r(NULL, ",", &saveptr); } ``` This code will print: ``` lopez John Doe jane Doe ```
The strtok_r function returns a null pointer when there are no more tokens to be extracted. The initial call to strtok_r is required to initialize the saveptr, and subsequent calls use null as the input string to extract the next token.
Handling Multi-byte Character Sets
If your string contains multibyte character sets, you can use the strtok_r() function from the libicui18n library, which handles multibyte character sets more efficiently. Here's an example:
```c
#include
Adjusting the delimiter and locale allows you to parse strings according to specific rules, making strtok_r a versatile tool for string manipulation.
Advantages and Use Cases of strtok_r
The main advantages of using strtok_r over strtok include:
- The original string remains untouched, as strtok_r does not modify it.
- It preserves the position of the last read token, enabling efficient string parsing.
- It can handle multibyte character sets more effectively when used with the libicui18n library.
Some use cases for strtok_r include parsing CSV or TSV files, splitting strings based on delimiters like commas, spaces, or tabs, and tokenizing user input for further processing.
Preserving Original String and Position
One of the key advantages of strtok_r is that it preserves the original string and maintains the position of the last read token. This is particularly useful when you need to iterate over a string multiple times or when the string is shared among different parts of the code. Here's an example: ```c char str[] = "lopez John Doe jane Doe"; char *token; char *saveptr; strtok_r(str, " ", &saveptr); token = strtok_r(NULL, " ", &saveptr); while (token != NULL) { printf("%s\n", token); token = strtok_r(NULL, " ", &saveptr); } strtok_r(NULL, "", &saveptr); // Resets saveptr for another iteration /* Now the string and saveptr are in the same state as after the first call to strtok_r */ token = strtok_r(NULL, " ", &saveptr); ``` This example demonstrates how strtok_r allows multiple iterations over the same string while preserving the original string and the position of the last read token.
In conclusion, the strtok_r function is a robust and efficient tool for string parsing in C. Its ability to preserve the original string and maintain the position of the last read token makes it more versatile and safer than the strtok function. Understanding and utilizing strtok_r can significantly enhance your string manipulation capabilities in C, making your code cleaner, more efficient, and easier to maintain.
For more examples and detailed information, consider exploring the GNU C Library or other online resources dedicated to the strtok_r function. Happy coding!













