Victoria Ideas Back to catalog
a screen shot of a web page with an image of a line graph on it
Aug 28, 2026 Victoria

Strtok_r Example in C/C++: Mastering String Tokenization

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 ...

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.

a screen shot of a web page with an image of a line graph on it

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.

a blackboard with some diagrams on it

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.

i feel like this should be common sense.

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

strtok_r example

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

a line graph shows the number of students in each class

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 char str[] = " Lopez John Doe jane Doe "; char *token; char *saveptr; Locale *locale = usLocale(); strtok_r(str, " \t", &saveptr, locale); token = strtok_r(NULL, " \t", &saveptr, locale); while (token != NULL) { printf("%s\n", token); token = strtok_r(NULL, " \t", &saveptr, locale); } ``` This example uses the US English locale, which can be replaced with other locales as needed.

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

how to trade s & r in forex? info graphic by the trader's guide

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.
brainandbodyhealth_ on June 29, 2024: "Understanding the TLR and STNR Reflexes: Key Differences and Exercises 🧠✨ Ever wondered about the differences between the TLR and STNR reflexes? They may be similar but here’s a quick guide on how to test each reflex and what they’re there for. TLR Reflex✅ 1. Have your child stand with feet together and arms by their sides. 2. Tilt their head back and have them close their eyes. 3. Look for a backward sway when looking up and a forward sway when looki Landau Reflex Exercises, Reflex Exercises, Tlr Reflex Integration Exercises, Tonic Labyrinthine Reflex Tlr, Reflex Integration Exercises, Reflex Integration, Babinski Reflex Integration, Tlr Reflex Exercises, Reflex Integration Activities For Kids
three different types of crt in the process of retracing and revsaling
the four models of crt are shown in purple and blue, with arrows pointing to each
Contoh Grafik S&R Paling Popular
The Rhetorical Situation
HOW RSI WORKS
Stochastic RSI crossover 📌
two different pictures of the same person with one man's face in black and white
Support and Resistance Setups: Trendlines, Zones, and Entries

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!

More shots

a screen shot of a web page with an image of a line graph on it
a screen shot of a web page with an image of a line graph on it
a blackboard with some diagrams on it
a blackboard with some diagrams on it
i feel like this should be common sense.
i feel like this should be common sense.
strtok_r example
strtok_r example
a line graph shows the number of students in each class
a line graph shows the number of students in each class
how to trade s & r in forex? info graphic by the trader's guide
how to trade s & r in forex? info graphic by the trader's guide
brainandbodyhealth_ on June 29, 2024: "Understanding the TLR and STNR Reflexes: Key Differences and Exercises 🧠✨ Ever wondered about the differences between the TLR and STNR reflexes? They may be similar but here’s a quick guide on how to test each reflex and what they’re there for. TLR Reflex✅ 1. Have your child stand with feet together and arms by their sides. 2. Tilt their head back and have them close their eyes. 3. Look for a backward sway when looking up and a forward sway when looki Landau Reflex Exercises, Reflex Exercises, Tlr Reflex Integration Exercises, Tonic Labyrinthine Reflex Tlr, Reflex Integration Exercises, Reflex Integration, Babinski Reflex Integration, Tlr Reflex Exercises, Reflex Integration Activities For Kids
brainandbodyhealth_ on June 29, 2024: "Understanding the TLR and STNR Reflexes: Key Differences and Exercises 🧠✨ Ever wondered about the differences between the TLR and STNR reflexes? They may be similar but here’s a quick guide on how to test each reflex and what they’re there for. TLR Reflex✅ 1. Have your child stand with feet together and arms by their sides. 2. Tilt their head back and have them close their eyes. 3. Look for a backward sway when looking up and a forward sway when looki Landau Reflex Exercises, Reflex Exercises, Tlr Reflex Integration Exercises, Tonic Labyrinthine Reflex Tlr, Reflex Integration Exercises, Reflex Integration, Babinski Reflex Integration, Tlr Reflex Exercises, Reflex Integration Activities For Kids
three different types of crt in the process of retracing and revsaling
three different types of crt in the process of retracing and revsaling

Related topics