[cs13001] string::npos

Mikhail Nesterenko mikhail at cs.kent.edu
Tue Oct 16 12:04:14 EDT 2012



> 
> string b = "Great!"; b.find('g'); 
> 
> How can I return string::npos to state that the letter hasn't been found? This didn't work for me: if (!b.find('g')) { return string::npos; }
> 

String find functions that we studied are expected to return the
position (index of the element) that matches the items searched
for. What if the item is not found at all? In this case the find
functions return string::npos. For example:

   string b= "Great!";
   if (b.find('x') == string::npos) cout << "no x found\n";

Will print: 

   no x found

Thanks,
--
Mikhail


More information about the cs13001 mailing list