加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

输入一个单词和一个句子,判断单词是否在句子中(C++)

(2011-10-09 11:06:14)
标签:

杂谈

分类: C
思路简单,代码如下:

# include<iostream>
# include<string.h>
using namespace std;

bool FindFavorite( char[],char[]);

int main()
{
char phrase[80],word[15],answer[5];
bool Find_it;
do
{
cout<<"\n请输入你最喜欢的单词===>";
cin.getline(word,15);

cout<<"\n\n请输入一句话或者短语===>";
cin.getline (phrase,80);

Find_it= FindFavorite( phrase,word);
if( Find_it==true)
cout<<"\n找到了单词==>"<<word<<endl;
else
cout<<"\n未找到单词==>"<<endl;

cout<<"继续输入YES or NO";
cin.getline( answer, 5);
}
while(strcmp(answer,"yes")==0);

return 0;
}
bool FindFavorite(char phrase[],char word[])
{
bool result;
char *IsIthere;
IsIthere=strstr(phrase,word);
if (IsIthere==NULL)
result=false;
else
result=true;
return(result);
}

需要学习的是 strstr 函数。
char *strstr( char *str, const char *strSearch ); // C++ only
char *strstr( const char *str, const char *strSearch ); // C only
……
Returns the address of the first occurrence of the matching substring if successful, or NULL otherwise.

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有