c++: How do I use .find() in an if-else statement to know whether or not a string contains a word? I try setting the if parameter not equal to -1 because I'm trying to test for npos, but only the if(stringABC.find()!=-1) branch executes, and never the else.
c++: How do I use .find() in an if-else statement to know whether or not a string contains a word?
I try setting the if parameter not equal to -1 because I'm trying to test for npos, but only the if(stringABC.find()!=-1) branch executes, and never the else.
1-npos is a steady static member cost. It is the finest possible fee for an element of kind size_t and size_t is an unsigned indispensable kind, -1 is the largest workable representable cost for this kind.
2-This cost, while used because the cost for a len parameter within the string's member features and this regular defined with a value of -1.
3-find() is the pre-defined feature and it uses operator = for assessment. It finds the primary occurrence of the detail.
4- Now, take two string variables and compare them with the npos.
string stringABC ("I am C++ developer");
string stringABC2 ("am");
5-stringABC.find(stringABC2) != string::npos
6- Please check the following example with a screenshot.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images