输出字符串中某个字串出现的次数

来源:互联网 发布:阿里云域名解析方法 编辑:程序博客网 时间:2024/05/21 12:43

int substrcnt(char *str1,char *str2)
{
 if(*str2==0||*str1==0)
  return 0;
 int cnt=0;
 char *s2=str2,*s1=str1;
 while(*s1!=0)
 {
  while(*s2!=0&&*s1==*s2)
  {
   ++s1;++s2;
  }
  if(*s2==0)
   ++cnt;
  if(s2==str2)
   ++s1;
  s2=str2;
 }
 return cnt;
}