LearnFrenzy
Sign in Take a free test

std::string · Predict the Output · Question 4

replace with a different length Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

What is the output of the following C++ program on a 64-bit system?

main.cpp
#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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links