编写一个涵数,计算字符串中子串出现的次数

来源:百度知道 编辑:UC知道 时间:2024/06/02 23:38:50

//判断两字符串是否相等,相等返回1,不等返回0
int Judge(char *movePt,char *tempPt)
{
int i;

for(i = 0; i < strlen(tempPt); i++, movePt++)
{
if(*movePt != tempPt[i])
return 0;
}
return 1;
}
//计算子串出现的次数,str为原字符串,sub为子串
int StrCount(char *str,char *sub)
{
int count = 0;
char *move = str;

if( strlen(str) < strlen(sub) )
{
return 0;
}
else
{
while( strlen(move) >= strlen(sub) )
{
if( Judge(move, sub) )
{
count++;
}
move++;
}
}
return count;
}