First of all, I know there are many duplicates of this question but I have tried and tried and non have been able to solve my issue.
I have the following string
string s = "asdfqasdfp";
I need to loop through the string and find which sub string appears more than once. so in this case its
asdf
I have made the following code but I do not know why it doesn't work. I start from the full string and go down one at a time. I should get occurence value of 2.
int t = s.length();
for (int i = 0; i < s.length(); i++) {
string str = s.substr(0, t);
int occurence = 0;
size_t start = 0;
while ((start = s.find(str, start)) != string::npos) {
++occurence;
start += str.length();
}
if (occurence > 1) {
cout << occurence;
}
else {
--t;
}
}
EDIT: I only want the largest substring that the string contains, in this case
"asdf"