Search for a substring inside a string

In C ++, I have a phone book with many names, such as Sinatra, Frank, and I want the user to be able to enter any line length to scan the file for her. After a user enters a string of any desired length, how do I scan a whole string of "Sinatra, Frank" only for "Frank" or "Sinatra" or "atra" and see what names it belongs to?

+3
source share
4 answers

You can use the method std::string::find:

string s = "Sinatra, Frank";
string::sizetype index = s.find("Frank");

This gives you a match index (which in this case is 9).

+6
source

: ( "Sinatra, Frank" ", ..), - , node , ..

, strstr():

strstr(const char *s1, const char *s2)

s2 s1, .

; - , , , .

Ken > ( , , "" ), , ; , / , .

+2

You can use strstr()to search for one substring in a string.

+1
source

If it is a std::string, you can use the .find()string method "Sinatra, Frank"

0
source

All Articles