LearnFrenzy
Sign in Take a free test

std::string · Predict the Output · Question 10

std::reverse and std::sort on a string Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 10 Back to Exercise

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

main.cpp
#include <algorithm>
#include <iostream>
#include <string>

int main() {
    std::string s = "stream";
    std::string r(s.rbegin(), s.rend());
    std::sort(s.begin(), s.end());
    std::cout << r << ' ' << s << '\n';
    return 0;
}
  • A maerts aemrst Correct
  • B maerts stream
  • C stream aemrst
  • D maerts tsrmea
Correct Answer: Option A

Constructing a string from reverse iterators makes a reversed copy. Sorting arranges the characters by code: a e m r s t.

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