编写程序比较两个 string 类型的字符串,然后编写另一 个程序比较两个 C 风格字符串的值。
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;
const int str_size = 80; char *str1,*str2; str1 = new char[str_size]; str2 = new char[str_size]; if(str1 == NULL||str2 == NULL) { cout<<"No enough memory!"<<endl; return -1; } cout<<"Enter two strings:"<<endl; cin>>str1>>str2; int result = 0; 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; delete [] str1; delete [] str2;