我正在尝试获取一个函数来拆分包含多个单词的字符串,这些单词由 1 个或多个空格分隔,并将每个单词不带任何空格放入字符串数组的索引中。
我已经在谷歌上搜索了一段时间,似乎我需要 strtok 但我有点无能为力,有人能说明一下吗?
/* 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;
}