How can I find the substring of the entered string in C ++? For example, I entered the string: "Hello World", then I enter "llo" and if there is such a substring in the entered string first, then let it print true . Here is something you need to create, but I do not know how to give a search in the string for a match.

  • Do you need to use standard language tools or implement an efficient algorithm? If the first - then you answered below, if the second, then most likely it is worth starting from here . - northerner
  • I agree with the fact that above - I myself dig into this topic and came to a similar exit in the example. - chudo116

2 answers 2

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

    man strstr

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

    • can you give more details? And this is something very short and incomprehensible - navi1893
    • 3
      If the string is a string, then it is better not to. strstr works with char *, to which string must also be converted. See the answer @gkuznets - alexlz

    Protected by member user31688 Jun 5 '15 at 8:16 .

    Thank you for your interest in this issue. Since he collected a large number of low-quality and spam responses, which had to be deleted, now it’s necessary to have 10 reputation points on the site (the bonus for account association is not counted ).

    Maybe you want to answer one of the unanswered questions ?