Ale*_*ore 11
简单的方法:使用strtok()或strtok_r获取前两个标记,这将从字符串中删除它们,因此字符串本身将是您正在寻找的第三个标记.
困难的方法:自己解析:(
Strtok在C字符串库中,并且会改变原始字符串,所以要小心,如果字符串需要保持原样,请先复制该字符串.
可能的例子:
//#include <string.h>
char input[] ="first second third forth";
char delimiter[] = " ";
char *firstWord, *secondWord, *remainder, *context;
int inputLength = strlen(input);
char *inputCopy = (char*) calloc(inputLength + 1, sizeof(char));
strncpy(inputCopy, input, inputLength);
firstWord = strtok_r (inputCopy, delimiter, &context);
secondWord = strtok_r (NULL, delimiter, &context);
remainder = context;
printf("%s\n", firstWord);
printf("%s\n", secondWord);
printf("%s\n", remainder);
getchar();
free(inputCopy);
Run Code Online (Sandbox Code Playgroud)
这应该工作得很好,并且原始字符串unmutated是线程安全的.
归档时间: |
|
查看次数: |
15949 次 |
最近记录: |