我正在尝试使用一个函数来分割一个包含多个单词的字符串,这些单词由1个或多个空格分隔,并将每个单词没有任何空格放入字符串数组的索引中。
我已经谷歌搜索了一段时间,似乎我需要strtok但是我有点无能,有人会请光一些吗?
答案 0 :(得分:4)
/* strtok example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ");
}
return 0;
}