我想获得一个函数来分割一个字符串,该字符串包含几个由1或多个空格分隔的单词,并将每个单词没有任何空格放入一个索引字符串数组。将一个字符串中的字放入一个字符串数组中 - C编程
我一直在Google上搜索它,似乎我需要strtok,但我有点无知,有人请摆脱一些光?
我想获得一个函数来分割一个字符串,该字符串包含几个由1或多个空格分隔的单词,并将每个单词没有任何空格放入一个索引字符串数组。将一个字符串中的字放入一个字符串数组中 - C编程
我一直在Google上搜索它,似乎我需要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;
}
谢谢@ andro1d这个好开始。我能告诉strtok忽略\ n和\ t吗?例: 例如: - 这是一个样本\ t字符串。以产生结果: - This, a sample 字符串 – PhillToronto
因为它现在是它\ t单独放在一行。谢谢 :) – PhillToronto
的http://stackoverflow.com/questions/12582955/remove-whitespaces-tail-from-string-char –
不是真的,我不希望删除空格可能重复,我更关心的是获得单词 – PhillToronto