strtok()函数在C语言中是什么?

strtok()函数在C语言中是什么?

strtok()函数是头文件的一部分#include

strtok()函数的语法如下所示−

char* strtok(char* string, const char* limiter);登录后复制

我们可以期望从strtok()获得一个字符串列表。但是,该函数返回一个单独的字符串,因为在调用strtok(input, limiter)后,它将返回第一个标记。

但是我们必须一次又一次地在一个空的输入字符串上调用该函数,直到我们得到NULL为止!

通常情况下,我们会继续调用strtok(NULL, delim)直到它返回NULL。

示例

以下是C程序的strtok()函数示例:

在线演示

#include #include int main() { char input_string[] = "Hello Tutorials Point!"; char token_list[20][20]; char* token = strtok(input_string, " "); 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(NULL, " "); // Get the next token. Notice that input=NULL now! } // Print the list of tokens printf("Token List:

", token_list[i]); } return 0; }