Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why c++ .find() method is not working properly?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading