[Solved] Return the longest common Substring Function c ++


You should change your if condition. string::find returns the position of the found string or string::npos if no string was found. string::find does not work with true and false. true is converted to 1. Therefor b.find(subString)== true is only true if subString is found at position 1 in b. You can verify this behavior with

#include <iostream>
#include <string>

int main() {
    std::string a = "Text";

    if (a.find("Text") == true) std::cout << 1 << std::endl;
    if (a.find("ext") == true) std::cout << 2 << std::endl;
    if (a.find("xt") == true) std::cout << 3 << std::endl;
    if (a.find("t") == true) std::cout << 4 << std::endl;
    return 0;
}

Output is 2

Try

b.find(subString) != b.npos

to fix your problem.

6

solved Return the longest common Substring Function c ++