I am writing a code to determine how many characters in string s are also in j
Here is my code:
string j,s;
cin >> j >>s;
int count=0;
for(int i=0;i<j.size();++i){
if(s.find(j[i])){
++count;
}
}
cout << count+1 << endl;
The problem is that s.find is not work for j[0] and
When a user enters none as string s it perceives it to be the keyword none.
How can I fix this?
I am using clang 13.0.0
GNU nano 6.0 as my editor
>Solution :
string::find does not return a boolean, but the position of the matching character (from 0 to size()-1) or string::npos if it doesn’t find anything.
https://en.cppreference.com/w/cpp/string/basic_string/find
Use if(s.find(j[i]) != std::string::npos) instead.