字符串匹配问题:输入一个字符串,如何计算其中包含的连续给定的子字符串的个数?

例如输入字符串“ EFABCABCABCDABCDD ” , 给定子字符串“ ABC” ,输出是 3 。
函数原型: int countsub( char *str, char *subs ) 。
参数说明: str 保存输入的字符串的首地址, subs 保存需要统计的子字符串的首地址。
返回值:包含的连续子字符串的个数。

预设代码
countsub_H20.c
view plaincopy to clipboardprint?
/* 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 */
急求!!谢谢!!

料青山看我应如是
浏览 574回答 2
2回答

翻阅古今

#include<stdio.h>int countsub(char *str, char *ss);int main(void){char s1[1000] = { 0 }, s2[100] = { 0 };gets(s1);gets(s2);printf("%d\n", countsub(s1, s2));return 0;}int countsub(char *str, char*ss){int i=0; //记录有多少相同的字符串。char *s22 = ss; //标记ss字符串的原点while (*str != '\0'){while (*str == *ss){if (*str == '\0'){break;}str++;ss++;}if (*ss == '\0'){i++;}else{str++;}ss = s22;}return i;}

慕桂英4014372

#include<iostream>using namespace std;int size(char *sz){int i = 0;while (sz[++i] != NULL);return i;}int countsub(char *str, char *ss){int temp = 0; int count = 0;if (ss == NULL || str == NULL) return -1;int strLen = size(str);int ssLen = size(ss);for (int i = 0; i<strLen; i++){if (str[i] == ss[temp]){temp++;}else if (str[i] == ss[0]){temp = 1;}else{temp = 0;}if (temp == ssLen){count++;temp = 0;}}return count;}int main(){char op[2][50] = { {'a','s','a','s','a','s','a','s','a','a','s'}, {'a','s'} };cout << countsub(op[0],op[1])<<endl;}
打开App,查看更多内容
随时随地看视频慕课网APP