为什么这个程序只能输出一个以ed结尾的单词.怎么改才能输出所有的呢?

来源:百度知道 编辑:UC知道 时间:2024/05/25 19:29:07
#include<stdio.h>
#include<string.h>
int main()
{
const char *a[4]={"interested","good","excited","nice"};
const char *b="ed";
int i;
for(i=0;i<=3;i++)
{
if(strstr(a[i],b)=="ed")
printf("%s\n",a[i]);
}
getch();
}

参考答案一个做女人的痛苦:当她和她所爱的男人有了肉体关系以后,她就很自然地把这种关系视为一种永远,但男人却可以不同,他们可能只会觉得那是生存方式的又一种演绎。正如书上说的:男女之间,在没有婚姻的承诺前,还是保持简单的关系为好,否则,真的没有岁月可以回头。

void main()
{
const char *a[4]={"interested","good","excited","nice"};
const char *b="ed";
int i;
for(i=0;i<=3;i++)
{
if(strstr(a[i],b)!=NULL)
printf("%s\n",a[i]);
}

}

strstr返回指向第一次出现ed位置的指针,如果没找到则返回NULL
if(strstr(a[i],b)=="ed")应改为if(strstr(a[i],b)!=NULL)