字符串匹配问题:输入一个字符串,计算其中包含的连续给定的子字符串的个数。
例如输入字符串“ EFABCABCABCDABCDD ” , 给定子字符串“ ABC” ,输出是 3 。
函数原型: int countsub( char *str, char *subs ) 。
参数说明: str 保存输入的字符串的首地址, subs 保存需要统计的子字符串的首地址。
返回值:包含的连续子字符串的个数
分析:根据题意我们主要需要解决两个问题:
1.如何去找到原字符串中的子字符串
2.原字符串中“连续”的子字符串个数
#include<stdio.h>
int countsub( char *str, char *ss )
{
int count = 0, i = 0;
//count用来计算子字符串的个数,i用来计算连续的子字符串个数
char *p = str, *q = ss;
while