700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 统计一个子字符串在另一个字符串中出现的次数

统计一个子字符串在另一个字符串中出现的次数

时间:2020-08-02 21:36:22

相关推荐

统计一个子字符串在另一个字符串中出现的次数

统计一个长度为n的字符串在另外一个字符串中出现的次数。

例如:假定输入字符串为helloworldhelloworld,子字符串为he,则应输出2

代码如下:

#include <stdio.h>#include <conio.h>#include <stdlib.h>#include <string.h>int proc(char* ,char*);int main(){char str[81],sub[3];int n;system("CLS"); //<stdlib.h>printf("Input a main string : \n");gets(str);printf("Please input a sub string : \n");gets(sub);puts(str);puts(sub);n = proc(str,sub);printf("Output result is : %d\n",n);getch();}int proc(char* str,char* sub){int n;char* p,* r;n = 0;while (*str){p = str;r = sub;while (*r){if (*p == *r){p++;r++;}elsebreak;}if (*r == '\0')n++;str++;}return n;}

结果如下:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。