您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2024/4/24 3:52:43发布12次查看
strtok()函数是bbed3fed50f96ac7490cfc6a498c4bc5头文件的一部分#include bbed3fed50f96ac7490cfc6a498c4bc5
strtok()函数的语法如下所示−
char* strtok(char* string, const char* limiter);
输入一个字符串和一个分隔符字符限制器。strtok()将根据分隔字符将字符串分割成标记。
我们可以期望从strtok()获得一个字符串列表。但是,该函数返回一个单独的字符串,因为在调用strtok(input, limiter)后,它将返回第一个标记。
但是我们必须一次又一次地在一个空的输入字符串上调用该函数,直到我们得到null为止!
通常情况下,我们会继续调用strtok(null, delim)直到它返回null。
示例以下是c程序的strtok()函数示例:
在线演示
#include <stdio.h>#include <string.h>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:
"); for (int i=0; i < num_tokens; i++) { printf("%s
", token_list[i]); } return 0;}
输出当上述程序被执行时,它产生以下结果 −
token list:hellotutorialspoint!
输入字符串为 “hello tutorials point”,我们尝试按空格进行分词。
我们通过使用strtok(input, )来获取第一个标记。这里双引号是分隔符,是一个单个字符的字符串!
之后,我们通过使用strtok(null, )来继续获取标记,并循环直到从strtok()获取到null为止。
以上就是strtok()函数在c语言中是什么?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product