Home c++ Find a substring in a string

Find a substring in a string

Author

Date

Category

How can one find a substring of an entered string in C++? For example, I entered the line: “Hello World”, then I enter “llo” and if the first line in the input contains such a substring, then let it output true. This is something that needs to be created, but I don’t know how to search for a match in a string.


Answer 1, authority 100%

# include & lt; string & gt;
#include & lt; iostream & gt;
int main () {
std :: string s = "Hello world";
size_t pos = s.find ("ell");
if (pos! = std :: string :: npos)
std :: cout & lt; & lt; "Found at pos =" & lt; & lt; pos & lt; & lt; "\ n";
else
std :: cout & lt; & lt; "Not found \ n";
}


Answer 2, authority 55%

man strstr

Returns a pointer to the first occurrence of the search string in a string.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions