字符串匹配问题:输入一个字符串,计算其中包含的连续给定的子字符串的个数。
例如输入字符串“ EFABCABCABCDABCDD ” , 给定子字符串“ ABC” ,输出是 3 。
函数原型: int countsub( char *str, char *subs ) 。
参数说明: str 保存输入的字符串的首地址, subs 保存需要统计的子字符串的首地址。
返回值:包含的连续子字符串的个数。
预设代码
countsub_H20.c
- /* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
- #include <stdio.h>
- int countsub( char *str, char *ss );
- main( )
- {
- char s1[1000] = {0}, s2[100] = {0};
- gets(s1);
- gets(s2);
- printf("%d\n", countsub( s1, s2 ) );
- }
- /* PRESET CODE END - NEVER TOUCH CODE ABOVE */
#include <string.h>
int countsub( char *str, char *ss )
{
int max=0,count=0,l=strlen(ss);char *p=str+l,temp;
while(*(p-1))
{
temp=*p;*p='\0';
if(strcmp(str,ss)) { str++;*p++=temp;count=0;}
else
{
count++;if(max<count) max=count;
str+=l;*p=temp;p+=l;
}
}
return max;
}
不能用while(*p)判断结束,*p='\0'是最后一组