//编写一段程序读入两个字符串,比较其是否相等并输出结果。如果不等,输出较大的那个字符串。
#include<iostream>
#include<string>
using namespace std;
int main( )
{
string s1, s2;
//提示用户输入字符串
cout << "请输入两个字符串 : " << endl;
cin >> s1 >> s2;
//比较两个字符串
if (s1 == s2)
cout << "两个字符串相等 " << endl;
else if (s1 > s2)
cout << s1 << endl;
else
cout << s2 << endl;
system("pause");
return 0;
}
//2、改写上述程序,比较输入的两个字符串的长度是否相等,如果不等,输出长度较大的那个字符串。
#include<iostream>
#include<string>
using namespace std;
int main( )
{
string s1, s2;
//提示用户输入两个字符串
cout << " 请输入两个字符串 " << endl;
cin >> s1 >> s2;
//比较两个字符串的长度
auto len1 = s1.size();
auto len2 = s2.size();
if (len1 == len2)
cout << s1 << " 和 " << s2 << " 的长度都是 " << len1 << endl;
else if (len1 > len2)
cout << s1 << " 的长度比 " << s2 << " 多 " << len1 - len2 << endl;
else
cout << s1 << " 的长度比 " << s2 << " 少 " << len2 - len1 << endl;
system("pause");
return 0;
}