Question 4
Back to Exercise
What is the output of the following C++ program on a 64-bit system?
#include <iostream>
#include <string>
int main() {
std::string s = "I love tea";
s.replace(7, 3, "coffee");
s.replace(2, 4, "like");
std::cout << s << ' ' << s.size() << '\n';
return 0;
}
- A I like coffee 13 Correct
- B I like coffee 10
- C I lovecoffee 12
- D I like tea 10
Correct Answer: Option A
replace(pos, len, text) may change the length: "tea" (3) becomes "coffee" (6), then "love" becomes "like". 13 characters in total.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!