Home Backend Development C++ The strtok_r() function is a function in C language. Its function is to split a string into a series of substrings.

The strtok_r() function is a function in C language. Its function is to split a string into a series of substrings.

Aug 26, 2023 am 09:45 AM
substring split string strtok_r()

The strtok_r() function is a function in C language. Its function is to split a string into a series of substrings.

This function is similar to the strtok() function. The only key difference is _r, which is called a reentrant function.

A reentrant function is a function that can be interrupted during execution. This type of function can be used to resume execution.

Thus, reentrant functions are thread-safe, which means they can be safely interrupted by threads without causing any damage.

strtok_r() function has an extra parameter called context. This way the function can be restored in the correct location.

The syntax of the strtok_r() function is as follows:

#include <string.h>
char *strtok_r(char *string, const char *limiter, char **context);
Copy after login

Example

The following is a C program using the

strtok_r() function -

Live demonstration

#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; // Index to token list. We will append to the list
   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:</p><p>");
   for (int i=0; i < num_tokens; i++) {
      printf("%s</p><p>", token_list[i]);
   }
   return 0;
}
Copy after login

Output

When the above program is executed, the following results will be produced-

Token List:
Hello
Tutorials
Point
Copy after login

The above is the detailed content of The strtok_r() function is a function in C language. Its function is to split a string into a series of substrings.. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Count the number of occurrences of a substring recursively in Java Count the number of occurrences of a substring recursively in Java Sep 17, 2023 pm 07:49 PM

Given two strings str_1 and str_2. The goal is to count the number of occurrences of substring str2 in string str1 using a recursive procedure. A recursive function is a function that calls itself within its definition. If str1 is "Iknowthatyouknowthatiknow" and str2 is "know" the number of occurrences is -3. Let us understand through examples. For example, input str1="TPisTPareTPamTP", str2="TP"; output Countofoccurrencesofasubstringrecursi

How to use the LOCATE function in MySQL to find the position of a substring in a string How to use the LOCATE function in MySQL to find the position of a substring in a string Jul 25, 2023 am 09:45 AM

How to use the LOCATE function in MySQL to find the position of a substring in a string. In MySQL, there are many functions that can be used to process strings. Among them, the LOCATE function is a very useful function that can be used to find the position of a substring in a string. The syntax of the LOCATE function is as follows: LOCATE(substring,string,[position]) where substring is the substring to be found and string is the substring to be found.

The strtok_r() function is a function in C language. Its function is to split a string into a series of substrings. The strtok_r() function is a function in C language. Its function is to split a string into a series of substrings. Aug 26, 2023 am 09:45 AM

This function is similar to the strtok() function. The only key difference is _r, which is called a reentrant function. Reentrant functions are functions that can be interrupted during execution. This type of function can be used to resume execution. Therefore, reentrant functions are thread-safe, which means they can be safely interrupted by threads without causing any damage. The strtok_r() function has an extra parameter called context. This way the function can be restored in the correct location. The syntax of the strtok_r() function is as follows: #include<string.h>char*strtok_r(char*string,constchar*limiter,char**

PHP returns the string from the start position to the end position of a string in another string PHP returns the string from the start position to the end position of a string in another string Mar 21, 2024 am 10:31 AM

This article will explain in detail how PHP returns the string from the start position to the end position of a string in another string. The editor thinks it is quite practical, so I share it with you as a reference. I hope you will finish reading this article. You can gain something from this article. Use the substr() function in PHP to extract substrings from a string. The substr() function can extract characters within a specified range from a string. The syntax is as follows: substr(string,start,length) where: string: the original string from which the substring is to be extracted. start: The index of the starting position of the substring (starting from 0). length (optional): The length of the substring. If not specified, then

How to use the split() function in Python 2.x to split a string according to the specified delimiter How to use the split() function in Python 2.x to split a string according to the specified delimiter Jul 31, 2023 pm 12:21 PM

How to use the split() function in Python2. Python provides the split() function to achieve this requirement. This article will introduce how to use the split() function to split a string according to the specified delimiter. The split() function is a built-in function of Python strings and is used to

PHP8.1's new str_contains function: quickly determine whether a substring exists PHP8.1's new str_contains function: quickly determine whether a substring exists Jul 07, 2023 pm 01:18 PM

PHP8.1’s new str_contains function: quickly determine whether a substring exists. In the latest PHP8.1 version, a very convenient function str_contains has been added. Its function is to quickly determine whether a string contains another string. substring. Compared with the previous strpos function, the str_contains function is more concise and easy to use, which can greatly improve development efficiency. This article will introduce to you how to use the str_contains function and

PHP Regular Expression: How to extract specific characters from a string to a substring at the end PHP Regular Expression: How to extract specific characters from a string to a substring at the end Jun 22, 2023 pm 05:33 PM

Regular expressions are a powerful text processing tool that can be used to match strings in specific patterns. In PHP, regular expressions are commonly used in string processing, form validation, search and replacement, etc. This article will introduce how to use PHP's regular expressions to extract specific characters from a string to the substring at the end. First, let's look at an example. Suppose we have a string $str that contains multiple URLs starting with "http://" and we want to extract these URLs and store them in a

Palindromic substring query in C++ Palindromic substring query in C++ Sep 22, 2023 am 09:05 AM

In this tutorial, we need to solve a palindrome substring query for a given string. Solving palindrome substring queries is much more complex than solving regular queries in C++. It requires more complex code and logic. In this tutorial, we have provided string str and Q substring [L...R] queries, each of which has two values ​​L and R. Our goal is to write a program that solves a query to determine if substring[L...R] is a palindrome. We have to determine whether the substring formed in the range L to R is a palindrome to solve each query. For example-Let'sinput"abbbabaaaba"asourinputstring.Thequer

See all articles