LearnFrenzy
Sign in Take a free test

Modern C++ Features (C++11 to C++23) · Predict the Output · Question 6

Slicing text with string_view Answer, explanation & discussion

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

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

main.cpp
#include <iostream>
#include <string_view>

int main() {
    std::string_view url = "https://learnfrenzy.com/programming/cpp";
    auto scheme = url.substr(0, url.find("://"));
    url.remove_prefix(scheme.size() + 3);
    auto host = url.substr(0, url.find('/'));
    auto path = url.substr(host.size());
    std::cout << scheme << '|' << host << '|' << path << '|' << path.size() << '\n';
    return 0;
}
  • A https|learnfrenzy.com|/programming/cpp|16 Correct
  • B https|learnfrenzy.com|programming/cpp|15
  • C https://|learnfrenzy.com|/programming/cpp|16
  • D https|learnfrenzy|.com/programming/cpp|20
Correct Answer: Option A

substr and remove_prefix on a string_view only adjust a pointer and a length – the original characters are never copied.

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