The problem with your function is that you are checking that:
Hellois substring ofHelloellois substring ofellollois substring ofllo- ...
of course this matches 5 times in this case.
What you really need is:
- For each position
iofstr - check if the substring of
strstarting atiand oflength = comp.size()is exactlycomp.
The following code should do exactly that:
size_t countMatches(const string& str, const string& comp){ size_t count = 0; for (int j = 0; j < str.size()-comp.size()+1; j++) if (comp == str.substr(j, comp.size())) count++; return count;}









