比较两个string类型的字符串
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main()
{
string str1,str2;
cout<<"Enter two strings: "<<endl;
cin>>str1>>str2;
if(str1>str2)
cout<<"\""<<str1<<"\""<<"is bigger than "<<"\""<<str2<<"\""<<endl;
else if(str1<str2)
cout<<"\""<<str2<<"\""<<"is bigger than "<<"\""<<str1<<"\""<<endl;
else
cout<<"They are equal"<<endl;
return 0;
}
比较两个C风格的字符串数组的值
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main()
{
const int str_size=80;
char *str1,*str2;
str1=new char[str_size];
str2=new char[str_size];
cout<<"Enter two strings:"<<endl;
cin>>str1>>str2;
int result;
result=strcmp(str1,str2);
if(result>0)
cout<<"\""<<str1<<"\""<<"is bigger than "
<<"\""<<str2<<"\""<<endl;
else if(result<0)
cout<<"\""<<str2<<"\""<<"is bigger than "
<<"\""<<str1<<"\""<<endl;
else
cout<<"They are equal."<<endl;
return 0;
}