Avatar ·

How to check if a string contains a substring in C++?

Hi everyone! I just started learning C++ and ran into a problem: how to check if one string contains another string? In other languages, like Python, this is done simply using the `in` operator or the `.find()` method. But in C++, I'm a bit confused. I have a variable `std::string text = "Hello, world!";` and I need to check if it contains the substring `"world"`. I tried writing this code: ```cpp #include #include int main() { std::string text = "Hello, world!"; std::string substr = "world"; if (text.find(substr)) { std::cout << "Found!" << std::endl; } else { std::cout << "Not found" << std::endl; } return 0; } ``` But this code doesn't work correctly: when the substring is present, it sometimes outputs "Not f