int fun(char *str,char *substr)
{
int counter=0;
for(int i=0;str[i+1]!='\0';i++)
{
if(str[i]==substr[0]&&str[i+1]==substr[1])
counter++;
}
return counter;
}
int fun(char *str,char *substr)
{
int n = 0;
if (2 != strlen(substr)) //如果子字符串长度不为2,返回0
{
printf("子字符串长度错误!\n");
return 0;
}
while (1)
{
str = strstr(str, substr);
if (NULL != str)
{
str+=2;
n++;
}
else
{
break;
}
}
return n;
}
int fun(char *str,char *substr)
{
int cnt;
char* pszStr;
cnt = 0;
pszStr = str;
while('\0' != *pszStr)
{
if(*pszStr == *substr && *(pszStr+1) == *(substr+1))
{
cnt++;
// 假设字符串sss中ss出现的次数算1次,则使用此句
// 算2次则把此句去掉
pszStr++;
}
pszStr++;
}
return cnt;
}
//---------------------------------------------------------------------------
#include
#include
int fun(char *str,char *substr)
{
int i,j,cnt=0;
const int lc=strlen(str)-strlen(substr);
for (i = 0; i
if (str[i+j]!=substr[j]) break;
if (substr[j]=='\0')
++cnt;
}
return cnt;
}
main()
{ char str[81],substr[81]; int n;
gets(str);
gets(substr);
puts(str);
puts(substr);
n=fun(str,substr);
printf("n=%d\n\n",n);
}
//---------------------------------------------------------------------------