Created
November 9, 2013 17:36
-
-
Save dgodfrey206/7387773 to your computer and use it in GitHub Desktop.
A function for find if a string is a substring of another string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
bool is_substring_of(const std::string& substr, const std::string& str) | |
{ | |
auto it = substr.begin(); | |
for (auto const& c : str) | |
{ | |
if (c == *it) | |
++it; | |
else | |
it = substr.begin(); | |
if (it == substr.end()) | |
return true; | |
} | |
return false; | |
} | |
int main() | |
{ | |
std::string apple = "apple"; | |
std::string sause = "myapplesause"; | |
std::cout << std::boolalpha << is_substring_of(apple, sause); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment