R S*_*hko 13
char *token;
char *state;
for (token = strtok_r(input, "&", &state);
token != NULL;
token = strtok_r(NULL, "&", &state))
{
...
}
Run Code Online (Sandbox Code Playgroud)
我会这样做(使用strchr()):
#include <string.h>
char *data = "this&&that&other";
char *next;
char *curr = data;
while ((next = strchr(curr, '&')) != NULL) {
/* process curr to next-1 */
curr = next + 1;
}
/* process the remaining string (the last token) */
Run Code Online (Sandbox Code Playgroud)
strchr(const char *s, int c)返回指向cin 的下一个位置的指针s,或者NULL如果c未找到,则返回指针s.
strtok()但是,您可以使用我不喜欢的strtok(),因为:
"a&&b&c",返回的令牌"a","b"和"c".请注意,之后没有空令牌"a".