#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static char *olds;
static char *save_ptr;
char *strtok1(char *s, const char *delim) {
char *token;
if (s == NULL) s = save_ptr;
/* Scan leading delimiters. */
s += strspn(s, delim); /*s中不包含任一delim中字符的首位置*/
if (*s == '\0')
return NULL;
/* Find the end of the token. */
token = s;
s = strpbrk(token, delim); /*找到首次匹配到delim中字符的指针*/
if (s == NULL)
/* This token finishes the string. */
save_ptr = strchr(token, '\0');
else {
/* Terminate the token and make *SAVE_PTR point past it. */
*s = '\0';
save_ptr = s + 1;
}
return token;
}
int main(void)
{
char buf[] = "nd:disna:dsnakn";
char *q = ":";
char *p = strtok1(buf,q);
while(p != NULL)
{
printf("%s\n",p);
p = strtok1(NULL,q);
}
// printf("%d\n",strspn(p,q));
// printf("%s\n",rc);
return 0;
}
strtok实现原理
最新推荐文章于 2022-08-12 22:22:59 发布
本文展示了一个使用C语言自定义实现的strtok函数,该函数用于字符串分割,通过指定的分隔符将字符串切分为多个子串。代码中包含了完整的函数定义及主函数调用示例。
4373

被折叠的 条评论
为什么被折叠?



