LearnFrenzy
Sign in Take a free test

std::string · Predict the Output · Question 11

Counting characters and find_first_of Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 11 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 = "Mississippi";
    std::cout << std::count(s.begin(), s.end(), 's') << ' '
              << s.find_first_of("ps") << ' '
              << s.find_last_not_of('i') << ' '
              << s.find_first_not_of("Mis") << '\n';
    return 0;
}
  • A 4 2 9 8 Correct
  • B 4 2 10 8
  • C 4 8 9 8
  • D 3 2 9 8
Correct Answer: Option A

Four s characters. The first p or s is at index 2. The last character that is not i is the p at index 9. The first character outside {M, i, s} is the p at index 8.

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