LearnFrenzy
Sign in Take a free test

std::string · Predict the Output · Question 3

substr with one and two arguments Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 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 = "programming";
    std::cout << s.substr(3) << ' ' << s.substr(0, 3) << ' ' << s.substr(3, 4) << ' ' << s.substr(8, 100) << '\n';
    return 0;
}
  • A gramming pro gram ing Correct
  • B gramming pro gra ing
  • C ramming pro gram ing
  • D gramming prog gram ing
Correct Answer: Option A

substr(pos) takes the rest; substr(pos, n) takes n characters; a count past the end is simply clipped.

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