【3.2】编写一段程序从标准输入中一次读入一整行,然后修改该程序使其一次读入一个词。
#include<iostream>
#include<string>
using namespace std;
void main()
{
string line,word;
if(getline(cin,line))
cout<<line<<endl;
if(cin>>word)
cout<<word<<endl;
}
【3.4】编写一段程序读入两个字符串,比较其是否相等并输出结果。如果不相等,输出较大的那个字符串。改写上述程序,比较输入的两个字符串是否等长,如果不等长,输出长度较大的那个字符串。
#include<iostream>
#include<string>
using namespace std;
void main()
{
string s1,s2,temp;
cin>>s1>>s2;
if(s1!=s2)
temp=s1>s2?s1:s2;
cout<<temp;
}
#include<iostream>
#include<string>
using namespace std;
void main()
{
string s1,s2,temp;
cin>>s1>>s2;
if(s1.size()!=s2.size())
temp=s1.size()>s2.size()?s1:s2;
cout<<temp;
}
【3.5】编写一段程序从标准输入中读入多个字符串并将它们连接在一起,输出连接成的大字符串。然后修改上述程序,用空格把输入的多个字符串分隔开来。