-2

嘿我目前正在学习c语言。是否有可能将char*类型变量(其中包含一些由空格分隔的单词)转换为字符串数组(在 c 中它是char*[]),这样原始变量上的每个单词都将位于新数组中的不同索引中?

4

1 回答 1

1

C 库函数strtok()

char * strtok ( char * str, const char * delimiters );

#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;
}
于 2013-10-05T15:50:07.560 回答