Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将字符串中的单词放入字符串数组-c编程_C_Arrays_String_String.h - Fatal编程技术网

将字符串中的单词放入字符串数组-c编程

将字符串中的单词放入字符串数组-c编程,c,arrays,string,string.h,C,Arrays,String,String.h,我试图得到一个函数来分割一个字符串,该字符串包含几个由1个或多个空格分隔的单词,并将每个没有空格的单词放入字符串数组的索引中 我在谷歌上搜索了一段时间,似乎我需要strtok,但我有点不知所措,有人能帮我解释一下吗?/*strtok示例*/ /* strtok example */ #include <stdio.h> #include <string.h> int main () { char str[] ="- This, a sample string.";

我试图得到一个函数来分割一个字符串,该字符串包含几个由1个或多个空格分隔的单词,并将每个没有空格的单词放入字符串数组的索引中

我在谷歌上搜索了一段时间,似乎我需要strtok,但我有点不知所措,有人能帮我解释一下吗?

/*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;
}
#包括 #包括 int main() { char str[]=“-这是一个示例字符串。”; char*pch; printf(“将字符串\%s\”拆分为标记:\n”,str); pch=strtok(str,“”); while(pch!=NULL) { printf(“%s\n”,pch); pch=strtok(空,“”); } 返回0; }
/*strtok示例*/
#包括
#包括
int main()
{
char str[]=“-这是一个示例字符串。”;
char*pch;
printf(“将字符串\%s\”拆分为标记:\n”,str);
pch=strtok(str,“”);
while(pch!=NULL)
{
printf(“%s\n”,pch);
pch=strtok(空,“”);
}
返回0;
}

可能重复的not really,我不想删除空格,我更关心的是获得单词可能重复的not really,我不想删除空格,我更关心的是获得单词Hank you@andro1d这是一个好的开始。我能告诉strtok忽略\n和\t吗?例如:-这是一个示例\t字符串。要产生结果:-这是一个示例字符串,因为它现在将\t单独放置在一行中。谢谢:)谢谢@andro1d这是个好的开始。我能告诉strtok忽略\n和\t吗?例如:-这是一个示例\t字符串。要产生结果:-这是一个示例字符串,因为它现在将\t单独放置在一行中。谢谢:)