(updated )

Accelerated C++ Chapter 1 – Working With Strings

Solutions to exercises in Chapter 1 of Accelerated C++, “Working with strings.”

Exercise 1-1

Are the following definitions valid? Why or why not?

const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";

Yes, these declarations are valid. The first line initializes the variable hello to the string "Hello". The second line initializes the variable message with a concatenation of hello, the string ", world", and the string "!".

The second line works because the + operator is left-associative. First it concatenates hello and ", world", which yields a std::string value. This value is then concatenated with "!" to yield the value with which message is initialized.

Exercise 1-2

Are the following definitions valid? Why or why not?

const std::string exclam = "!";
const std::string message = "Hello" + ", world" + exclam;

The first line is valid because it initializes the variable exclam with the value "!". The second line is not valid, however. The reason is that the + operator is left-associative. This means that it is being used to concatenate "Hello" and ", world", which is not legal because one of the operands must be a std::string object. In the exercise, "Hello" and ", world" are both string literals.

Exercise 1-3

Is the following program valid? Is so, what does it do? If not, why not?

Yes, this is a valid program. It contains two scopes, and in each scope there is a variable of type std::string named s. The variable ceases to exist at the end of each scope. The output of the program is as follows:

a string
another string

Exercise 1-4

What about this one? What if we change }} to };} in the third line from the end?

Yes, this is a valid program. There is a scope nested inside of an outer scope. Because the inner scope defines a variable named s, it hides the variable of the same name that is defined in the outer scope.

Changing the }} to };} in the third line from the end will have no effect on the meaning of the program. The semicolon denotes an empty statement in this situation.

The output of the program is as follows:

a string
another string

Exercise 1-5

Is this a valid program? If so, what does it do? If not, say why not, and rewrite it to be valid.

This is not a valid program because the variable x is declared inside a scope, and it is unavailable when the scope ends. Because of this, the line which attempts to output x will not compile.

To make this program succeed, the simplest fix is to keep the code in one scope by removing the innermost braces, as follows:

The output of the program is as follows:


a string
a string, really

Exercise 1-6

What does the following program do if, when it asks you for input, you type two names (for example, Samuel Beckett)? Predict the behavior before running the program, then try it.

The first std::cin line will read input until it encounters a space, and store the result in the variable name. The rest of the input remains buffered. The second std::cin line will read the remaining name after the space without pausing to ask for more input. The output of the program is as follows:

What is your name? Samuel Beckett
Hello, Samuel
And what is yours? Hello, Beckett; nice to meet you, too!