C语言实现子串出现次数统计

/*------------------------------------------------

【程序设计】


功能:编写一个函数,该函数可以统计一个长度为2的字符

串在另一个字符串中出现的次数。

例如:假定输入的字符串为:asdasasdfgasdaszx67asdmklo,

字符串为:as,则应输出6。

------------------------------------------------*/

#include<stdio.h>

#include<string.h>

#include<conio.h>

void wwjt();

int fun(char* str, char* substr)

{

/**********Program**********/

char* p;

int i = 0;

while ((p = strstr(str, substr)) != NULL)

{

i++;

str = p+1;

}

return i;

/********** End **********/

}

int main()

{

char str81, substr3;

int n;

printf("输入主字符串: ");

gets(str);

printf("输入子字符串: ");

gets(substr);

puts(str);

puts(substr);

n = fun(str, substr);

printf("n=%d\n", n);

wwjt();

return 0;

}

void wwjt()

{

FILE* IN, * OUT;

int n;

char i200;

IN = fopen("in.dat", "r");

if (IN == NULL)

{

printf("Read FILE Error");

}

OUT = fopen("out.dat", "w");

if (OUT == NULL)

{

printf("Write FILE Error");

}

fscanf(IN, "%s", i);

n = fun(i, "as");

fprintf(OUT, "%d", n);

fclose(IN);

fclose(OUT);

}

相关推荐
美团技术团队10 分钟前
LongCat 开源 VitaBench 2.0:长期动态智能体基准新标杆
人工智能·算法
To_OC17 小时前
LC 207 课程表:刚学图论那会儿,我连这是拓扑排序都没看出来
javascript·算法·leetcode
To_OC17 小时前
LC 208 实现 Trie 前缀树:曾被名字劝退,写完发现是送分题
javascript·算法·leetcode
BadBadBad__AK19 小时前
线段树维护区间 k 次方和
c++·数学·算法·stl
_清歌1 天前
DSpark 深度解读:DeepSeek-V4 如何用「半自回归」把推理速度提升 85%
算法
统计实现局1 天前
SVD 的三步走:双对角化、Givens 收敛、排序
算法
躬行见万象1 天前
《VLA 系列》UniLab 强化训练 | G1 机器人 |复现
算法
统计实现局1 天前
对称不定分解(Bunch-Kaufman):为什么 Cholesky 不够用
算法
统计实现局1 天前
dqrsl 拆解:拿着 QR 结果能算出哪 5 种东西
算法