Exercise 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;
}
Exercise 3.3 The
string object will automatically ignore the white space at the beginning (ie, space character, newline character, tab character, etc.) and read from the first character until it encounters the next white space. The
getline function automatically ignores the white space and does not regard it as a judgment. Conditions for the end of the function
Exercise 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;
}
}
Exercise 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;
}