第一题:母串里面查找子串
(1)递归方法
#include <string.h>
int strstr_count(const char *s, const char *child)
{
char *p = strstr(s, child);
return p ? 1 + strstr_count(p+1, child) : 0;
}
(2)模板匹配方法
#include<string.h>
#include <assert.h>
int strstr_count(const char*Src,const char *child)
{
assert(Src!=NULL && child != NULL);
int total = 0;
char *p = strstr(Src,child);
while(p!=NULL)
{
total++;
p++;
p = strstr(p,child);
}
return total;
}