Home Common Problem Detailed explanation of strtok function usage

Detailed explanation of strtok function usage

Nov 28, 2023 am 10:15 AM
strtok function

The strtok function is a function used to split a string according to a specified delimiter. The split substrings can be returned one by one through multiple calls. It should be noted that the original string will be modified by the strtok function, which will replace the delimiter with the string terminator '\0', so the original string may have been modified after all substrings have been processed. If you need to keep the original string, you can create a copy before splitting.

Detailed explanation of strtok function usage

The strtok function is a string splitting function in C language. It is used to split a string according to the specified delimiter and return the split sub-characters. string. Its usage is as follows:

char *strtok(char *str, const char *delimiters)
Copy after login

str: ​​The string to be split, the string to be split is passed in when called for the first time, and NULL is passed in in subsequent calls.

delimiters: delimiter string, specifying the set of characters used to split the string.

The strtok function works as follows:

1. When calling for the first time, pass in the string to be split as a parameter and return the pointer of the first substring.

2. In subsequent calls, if NULL is passed in as the first parameter, the function will continue to split the string from the end of the previous call and return the pointer to the next substring.

3. When there are no more substrings to return, the function returns NULL.

The following is a simple example demonstrating the usage of strtok function:

#include
#include
int main() {
char str[] = "Hello, World! How are you?";
const char delimiters[] = " ,!";
// 第一次调用strtok
char *token = strtok(str, delimiters);
// 通过循环继续分割字符串
while (token != NULL) {
printf("%s\n", token);
// 后续调用strtok
token = strtok(NULL, delimiters);
}
return 0;
}
Copy after login

In the above example, we convert the string "Hello, World! How are you?" split according to spaces, commas and exclamation marks. First, call the strtok function for the first time and pass in the string to be split and the delimiter string. The function returns the pointer "Hello" of the first substring, and then Continue to call the strtok function through the loop, passing in NULL as the first parameter, and continue to split the string. Each time through the loop, the function returns the pointer to the next substring, until there are no more substrings to return, the function returns NULL, the loop ends.

It should be noted that the original string will be modified by the strtok function, which will replace the delimiter with the string terminator '\0', so after all substrings are processed , the original string may have been modified. If you need to keep the original string, you can create a copy before splitting.

In summary, the strtok function is a function that is used to divide the string according to the specified delimiter. The split function can return the split substrings one by one through multiple calls. By understanding the usage and precautions of the strtok function, you can easily handle the need for string splitting.

The above is the detailed content of Detailed explanation of strtok function usage. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)