#include <iostream>
#include <string>
bool Find(std::string input, const std::string &substring)
{
// If the substring length is bigger than input length, what's the point to continue?
if (substring.length() > input.length())
return false;
// If both arguments are equal, what's the point to continue?
if (input == substring)
return true;
int substr_length = substring.length(); // Find 'substring' length
unsigned int index = 0;
while (index < input.length())
{
std::string to_compare = "";
int f_pos = input.find(substring[0]); // Position in 'input' of the first 'substring' character
if (f_pos == input.npos) // Couldn't find?
return false;
for (int i = f_pos; i <= substr_length + f_pos - 1; i++)
to_compare += input[i]; // Build a substring
// Compare it with the 'substring' parameter
if (to_compare != substring)
{
int find = input.find(substring[0]); // Position in 'input' of the first 'substring' character
if (find != input.npos)
input.erase(find, substr_length - 1); // Erase the substring if it wasn't equal with 'substring'
}
else
return true;
index++;
}
// To avoid a warning
return false;
}
int main()
{
// A simple test:
if (Find("alex", "lex"))
std::cout << "Found substring!" << std::endl;
else
std::cout << "Didn't find substring!" << std::endl;
return 0;
}