C++ 字符串 2-- 18.6~18.10 string型字符串

#include <iostream>
#include <string>
using namespace std;
/*---------------------------------
     18-02 18.6~18.10 string型字符串
---------------------------------*/
int check(char des[],char sor[]);
int main()
{
string str="string字符串";
char ch[]="char字符串";
cout<<str<<endl;
cout<<ch<<endl;
cout<<"请输入“狗”的英文单词:"<<endl;
cin>>str;
if(str=="dog")
{cout<<"狗:"<<str<<endl;}
else
{cout<<"输入错误"<<endl;}
cout<<"请输入“猪”的英文单词:"<<endl;
cin>>ch;
//if(ch=="pig")           //这种方式是错误,相当于是在比较两个地址
if(0==check("pig",ch))    //自定义的比较函数
//if(0==strcmp("pigf",ch)) //库函数
{cout<<"猪:"<<ch<<endl;}
else
{cout<<"输入错误"<<endl;}


return 0;
}


int check(char des[],char sor[])
{
bool quit=false;
if(strlen(des)!=strlen(sor))
return 1;
for(int i=0;i<strlen(des);i++)
{
if(des[i]!=sor[i])
{
quit =true;
break;
}
}
if(quit==false)
{
return 0;
}
return 1;


}

运行结果:

string字符串
char字符串
请输入“狗”的英文单词:
dog
狗:dog
请输入“猪”的英文单词:
pig
猪:pig
Press any key to continue

猜你喜欢

转载自blog.csdn.net/paulliam/article/details/80509590