我正在尝试编写一个代码,使用 strstr 提取和标记之间的所有单词/字符串。但似乎它只是停留在提取的第一个字符串上,即“快速”。提取第一个字符串后如何让代码继续运行?
#include <stdio.h>
#include <string.h>
int main()
{
char feed[] = "The <item> quick </item> brown <item> fox </item> jumps <item> over </item> the <item> lazy dog </item>";
const char needle[] = "<item>";
const char popo[] = "</item>";
char *ret;
char *ter;
int n;
n = 0;
while (feed[n] != '\0')
{
ret = strstr(feed, needle)+6;
ter = strstr(ret, popo);
size_t len = ter - ret;
char *res = (char*)malloc(sizeof(char)*(len+1));
strncpy(res, ret, len);
res[len] = '\0';
printf("%s",res);
n++;
}
return 0;
}