What will be the output of the following C++ code using find()?

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s = "BPSC TRE";
    cout << s.find("TRE");
    return 0;
}
                        

Options

  • A. 4
  • B. 5
  • C. 6
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

B. 5

Detailed Explanation

Correct Answer: Option B — 5

Explanation: find() returns the zero-based index of the first occurrence of the searched substring. In BPSC TRE, the substring TRE begins at index 5 because B, P, S, C, and the space occupy indices 0 through 4.