练习3.2
#include<iostream>
#include<string>
using namespace std;
int main()
{
string line;
while (getline(cin, line))
cout << line << endl;
string word;
while (cin >> word)
cout << word << ' ';
cout << endl;
}
练习3.3
string对象会自动忽略开头的空白(即空格符 换行符 制表符等)并从第一个字符读起,直到遇到下一处空白
getline函数会自动忽略空白符,不将其作为判定函数结束的条件
练习3.4
#include<iostream>
using namespace std;
int main()
{
string s1 = "Hello", s2 = "World";
if (s1 == s2)
cout << "equal" << endl;
else if (s1 > s2)
cout << s1 << " bigger" << endl;
else
cout << s2 << "bigger" << endl;
//modified
if (s1.size == s2.size())
cout << "same length" << endl;
else
{
if (s1 == s2)
cout << "equal" << endl;
else if (s1 > s2)
cout << s1 << " bigger" << endl;
else
cout << s2 << "bigger" << endl;
}
}
练习3.5
#include<iostream>
using namespace std;
int main()
{
string word;
string conj;
while (cin >> word)
conj += word;
cout << conj << endl;
//modefied
while (cin >> word)
conj += " " + word;
cout << conj << endl;
}