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.
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)
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; }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
