compute recursively the number of times
that sub appears in the string, without the
sub strings overlapping.
str_count("catcowcat", "cat") returns 2
str_count("catcowcat", "cow") returns 1
str_count("catcowcat", "dog") returns 0
please give me some idea to solve this problem
int str_count(string str, string sub)
{
int len = str.length();
int s_len = sub.length();
if(len < s_len) return 0;
else if(len >= s_len)
{
if(str.substr(0,s_len) == sub) return 1 + str_count(str.substr(s_len), sub);
else return 0 + str_count(str.substr(s_len), sub);
}
}
my code doesn't work for
str_count("cacatcowcat", "cat")
it return 0 for this case.