- 论坛徽章:
- 0
|
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(int ac, char *av[])
{
char str[]=" hello 大家好 world 你好 ";
char buf[64];
char *p[6],*p1;
int i,n;
printf("str = [%s] \n", str);
strcpy(buf, str);
p[0]=strtok(str," ");
n=1;
while((p1=strtok(NULL," ")))
{
p[n++]=p1;
}
for(i=0;i<n;i++)
{
printf("%s,%s\n",p[i],strstr(buf,p[i]));
// getchar();
}
printf("\n\n");
return 0;
}
执行结果
str = [ hello 大家好 world 你好 ]
hello,hello 大家好 world 你好
大家好,大家好 world 你好
world,world 你好
你好,你好
问题出在 strtok() 函数,该函数执行后,已经修改了输入缓冲区,将相应空格修改为字符串结束符,后面再执行 strstr 就出现了错误,无法再打印 str 缓冲区了。 |
|