嘿我正在学习c语言。是否有可能将char*
类型变量(包含一些由空格分隔的单词)转换为字符串数组(在c char*[]
中),这样原始变量上的每个单词都将位于不同的索引中在新阵列?
答案 0 :(得分: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;
}