Comparing occurrences of characters in a string C++

  c++

I am moving from Python to C++ and I am not well versed with the latter.

I have a string s, which contains exactly one character "a" and one character "b" while the other characters are redundant.
I want to know whether "a" occurs first or "b" does.
In Python, I would do it something like this

s = "??a????b"
if s.find("a") < s.find("b"):
    print("a")
else:
    print("b")

Also, for C++, I tried this:

#include<bits/stdc++.h>

using namespace as std;

int main(){

string s;
cin >> s;
bool isTrue = 1;
for(int i =0; i < s.size(); i++){

if(s[i] == "a"){break;}
else if(s[i] == "b"){isTrue = 0; break;}

}
cout << (isTrue ? "a" : "b") << endl;

}

But it gives an error pointing to the comparison between s[i] and "a" saying integer cannot be compared with a pointer. Kindly explain what is happenning.

I searched for find() function in c++ but things got out of bounds for me very soon. So kindly try explaining me using as less jargon and advanced libraries as possible how to solve the problem and what was wrong in my code.

P.S.: I am learning competitive programming so please don’t be shocked to see that include<bits/stdc++.h> up there.

Source: Windows Questions C++

LEAVE A COMMENT