Section 10.2 Check Point Questions7 questions 

10.2.1
To create a string "Welcome to C++", you may use a statement like this:
  string s1("Welcome to C++"); 
or this:
  string s1 = "Welcome to C++"; 
Which one is better?
10.2.2
Suppose that s1 and s2 are two strings, given as follows:
string s1("I have a dream");
string s2("Computer Programming");
Assume that each expression is independent. What are the results of the following expressions?
(1) s1.append(s2)
(2) s1.append(s2, 9, 7)
(3) s1.append("NEW", 3) 
(4) s1.append(3, 'N') 
(5) s1.assign(3, 'N') 
(6) s1.assign(s2, 9, 7) 
(7) s1.assign("NEWNEW", 3)
(8) s1.assign(3, 'N')
(9) s1.at(0)
(10) s1.length()
(11) s1.size()
(12) s1.capacity() 
(13) s1.erase(1, 2)
(14) s1.compare(s2) 
(15) s1.compare(2, 3, s2)
(16) s1.c_str()
(17) s1.substr(4, 8)
(18) s1.substr(4)
(19) s1.find('A')
(20) s1.find('a', 9)
(21) s1.replace(2, 4, "NEW")
(22) s1.insert(4, "NEW")
(23) s1.insert(6, 8, 'N')
(24) s1.empty()
10.2.3
Suppose that s1 and s2 are given as follows:
string s1("I have a dream");
string s2("Computer Programming");
Assume that each expression is independent. What are the results of the following expressions?
(1) s1[0]
(2) s1 = s2
(3) s1 = "C++ " + s2 
(4) s2 += "C++ " 
(5) s1 > s2 
(6) s1 >= s2 
(7) s1 < s2
(8) s1 <= s2
(9) s1 == s2
(10) s1 != s2
10.2.4
Suppose you entered New York when running the following programs. What would be the output?
(a)
#include <iostream>
#include <string>
using namespace std;

int main()
{
  cout << "Enter a city: ";
  string city;
  cin >> city;

  cout << city << endl;

  return 0;
}
 
(b)
#include <iostream>
#include <string>
using namespace std;

int main()
{
  cout << "Enter a city: ";
  string city;
  getline(cin, city);

  cout << city << endl;

  return 0;
}
10.2.5
Show the output of the following code (the replaceString function is defined in Listing 10.2).
string s("abcdabab"), oldSubStr("ab"), newSubStr("AAA");
replaceString(s, oldSubStr, newSubStr);
cout << s << endl;
10.2.6
If the replaceString function is returned from line 44 in Listing 10.2, is the returned value always false?
10.2.7
What is wrong in the following code? How do you fix it?
string s("New York");
if (s.find('P') >= 0)
{
  cout << "Character P is in " << s << endl;
}
else 
{
  cout << "Character P is not in " << s << endl;
}