#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实现原理
最新推荐文章于 2024-04-21 09:33:39 发布