C中带有分隔符的拆分字符串如何编写函数来拆分和返回C编程语言中带有分隔符的字符串的数组?char* str = "JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC";str_split(str,',');
3 回答
qq_遁去的一_1
TA贡献1725条经验 获得超7个赞
strsep
while ((token = strsep(&str, ","))) my_fn(token);
==
.
token
str
char *
// More general pattern:const char *my_str_literal = "JAN,FEB,MAR";char *token, *str, *tofree;tofree = str = strdup(my_str_literal); // We own str's memory now.while ((token = strsep(&str, ","))) my_fn(token);free(tofree);
str
token
str
strtok
strsep
strtok
strsep
strsep
strbrk
strsep
char *strsep(char **stringp, const char *delim) { if (*stringp == NULL) { return NULL; } char *token_start = *stringp; *stringp = strpbrk(token_start, delim); if (*stringp) { **stringp = '\0'; (*stringp)++; } return token_start;}
strsep
strtok
strsep
strtok
.
开心每一天1111
TA贡献1836条经验 获得超13个赞
int main(void) { char st[] ="Where there is will, there is a way."; char *ch; ch = strtok(st, " "); while (ch != NULL) { printf("%s\n", ch); ch = strtok(NULL, " ,"); } getch(); return 0;}
- 3 回答
- 0 关注
- 809 浏览
添加回答
举报
0/150
提交
取消