输入一个单词和一个句子,判断单词是否在句子中(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.
后一篇:无题