[英]Place words of a string into an array of strings - c programming
I am trying to get a function to split a string containing several words which are separated by 1 or more spaces and put each word without any spaces into an index of an array of strings. 我正在尝试获得一个函数,以拆分包含多个单词的字符串,这些单词由1个或多个空格分隔,并将每个不带任何空格的单词放入字符串数组的索引中。
I have been googling it for a while, it seems I need strtok but I am a bit clueless, would someone please shed some light? 我已经搜索了一段时间,似乎我需要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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.