子字符串在母字符串中出现的次数

#include<stdio.h>
int getcount(char *mystr,char *sub,char *ncount)
{
int ret=0;
char *p=mystr;
int tmpcount;
if(mystr==NULL||sub==NULL||ncount==NULL)
{
ret=-1;
printf("func getcount():%d(mystr==NULL||sub==NULL||ncount==NULL)\n",ret);
return ret;
}
do
{
p=strstr(p,sub); //指针达到下次查找的条件
if(p!=NULL)
{
tmpcount++;
p=p+strlen(sub);
}
else
{
break;
}
}
while(*p!='\0');
*ncount=tmpcount; //见解赋值,指针存在最大意义
return ret;

}


void main()
{
int ret=0;
int count=0;
char *p="11abcd222abcd333abcd444abcd";
char *a="abcd";
ret=getcount(p,a,&count);
if(ret!=0)
{
printf("func getcount() err%d\n",ret);
return ret;
}
printf("count:%d",count);

}

posted @ 2018-03-07 10:54  晴天麦芽糖  阅读(720)  评论(0编辑  收藏  举报