Solutions to exercises in Chapter 1 of Accelerated C++, “Working with strings.”
Exercise 1-1
Are the following definitions valid? Why or why not?
1 2 | const std::string hello = "Hello"; const std::string message = hello + ", world" + "!"; |
Solution
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?
1 2 | const std::string exclam = "!"; const std::string message = "Hello" + ", world" + exclam; |
Solution
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?
1 2 3 4 5 6 7 8 9 10 11 12 | #include <iostream> #include <string> int main() { { const std::string s = "a string"; std::cout << s << std::endl; } { const std::string s = "another string"; std::cout << s << std::endl; } return 0; } |
Solution
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:
1 2 | a string another string |
Exercise 1-4
What about this one? What if we change }} to };} in the third line from the end?
1 2 3 4 5 6 7 8 9 10 11 | #include <iostream> #include <string> int main() { { const std::string s = "a string"; std::cout << s << std::endl; { const std::string s = "another string"; std::cout << s << std::endl; }} return 0; } |
Solution
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.
1 2 3 4 5 6 7 8 9 10 11 | #include <iostream> #include <string> int main() { { const std::string s = "a string"; std::cout << s << std::endl; { const std::string s = "another string"; std::cout << s << std::endl; };} return 0; } |
The output of the program is as follows:
1 2 | 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.
1 2 3 4 5 6 7 8 9 10 11 12 | #include <iostream> #include <string> int main() { { std::string s = "a string"; { std::string x = s + ", really"; std::cout << s << std::endl; } std::cout << x << std::endl; } return 0; } |
Solution
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:
1 2 3 4 5 6 7 8 9 10 11 12 | #include <iostream> #include <string> int main() { { std::string s = "a string"; std::string x = s + ", really"; std::cout << s << std::endl; std::cout << x << std::endl; } return 0; } |
The output of the program is as follows:
1 2 | 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream> #include <string> int main() { std::cout << "What is your name? "; std::string name; std::cin >> name; std::cout << "Hello, " << name << std::endl << "And what is yours? "; std::cin >> name; std::cout << "Hello, " << name << "; nice to meet you, too!" << std::endl; return 0; } |
Solution
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:
1 2 3 | What is your name? <b>Samuel Beckett</b> Hello, Samuel And what is yours? Hello, Beckett; nice to meet you, too! |
Hey thanks for the solutions :)
Thanks for writing up the solutions, very very useful!
Thanks for this i just bought the book and they are kinda scarce as to finishing the solutions and what not!
Oh, where did you buy the book? I can’t find them in the shopping websites!
I just want to say thank.This is really helpful for me.
Great work expecting more solutions!
Thanks a lot.
If you could do all the problems, it will be much greater!
This is great, using the book to learn some C++, these will come in handy!
This is a big help, thanks for going to the trouble.
Thanks, helped a lot!! :-)