Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
Created November 9, 2013 17:36
Show Gist options
  • Save dgodfrey206/7387773 to your computer and use it in GitHub Desktop.
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
#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