Answers to Self-Review Exercises

  1. 21.1

    1. <string>.

    2. std.

    3. erase.

    4. find_first_of.

  2. 21.2

    1. True.

    2. True.

    3. True.

    4. False. A string is an object that provides many different services. A pointer-based string does not provide any services. Pointer-based strings are null terminated; strings are not necessarily null terminated. Pointer-based strings are pointers and strings are objects.

  3. 21.3

    1. Constructors for class string do not exist for integer and character arguments. Other valid constructors should be used—converting the arguments to strings if need be.

    2. Function data does not add a null terminator. Also, the code attempts to modify a const char. Replace all of the lines with the code:

      
      cout << name.substr(0, 3) + "-" + name.substr(4) << endl;
      

Get C++ How to Program, 10/e now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.