Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- const char* strstrM(const char* str, const char* search)
- {
- for (; *str; ++str)
- {
- for (int searchIdx{ 0 }; *(str + searchIdx) == *(search + searchIdx); ++searchIdx)
- {
- if (!*(search + searchIdx + 1)) { return str; }
- }
- }
- return nullptr;
- }
- int main()
- {
- char str[]{ "Hello, this beautiful this world! this is that but that is not thi$" };
- char search[]{ "this" };
- int foundCnt{ 0 };
- for (auto pos{ strstrM(str, search) }; pos; pos = strstrM(pos+1, search),++foundCnt)
- {
- std::cout << "\"" << search << "\" found at " << (pos - str) << '\n';
- }
- if ( foundCnt)
- {
- std::cout << "\"" << search << "\" found " << foundCnt << " times!\n";
- }
- else
- {
- std::cout << "\"" << search << "\" not found in \"" << str << "\"\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement