strtok简单实现

bool rav_chrInStr(char chr, const char* str) {
while (str) {
if (chr == str) return true;
str++;
}
return false;
}
char
rav_strtok(char
s, const char* delim) {
static char* text = NULL;
if (text == NULL) text = s;
if (text == NULL) return NULL;
char *head = text;
while (*text && !rav_chrInStr(*text, delim)) {
text++;
}
while (*text&&rav_chrInStr(*text, delim)) {
*text = ‘\0’;
text++;
}
if (*text == ‘\0’) text = NULL;
return head;
}
int main(void) {
char buf[] = “hello,boy@this,is@biubiubiu”;
char *temp = rav_strtok(buf, “,@”);
while (temp) {
printf("%s\n", temp);
temp = rav_strtok(NULL, “,@”);
}
return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42528287/article/details/85111474
今日推荐