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);

}

相关推荐
delishcomcn35 分钟前
AI视觉识别+分切算法:电化铝缺陷检测与裁切一体化解锁
人工智能·算法
触底反弹1 小时前
深入理解大模型采样:Temperature、Top-K、Top-P 的原理与实战
人工智能·算法·面试
雪碧聊技术1 小时前
力扣 LCR 091. 粉刷房子 —— 动态规划入门详解
算法·动态规划
CV-Climber4 小时前
检索技术的实际应用
人工智能·算法
hhzz4 小时前
Tiger AI Platform平台中增加人脸识别功能
图像处理·人工智能·算法·计算机视觉·大模型
从零开始的代码生活_5 小时前
C++ 继承详解:访问控制、对象模型、菱形继承与设计取舍
开发语言·c++·后端·学习·算法
TsingtaoAI6 小时前
3D高斯泼溅技术发展及其在具身智能领域的应用综述
人工智能·算法·ai·具身智能·高斯泼溅
atunet6 小时前
关于图算法中的连通分量检测与最小割问题7
算法
心运软件6 小时前
银行客户流失预测(Python 完整实战)
算法