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 str[81], substr[3];

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 i[200];

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

}

相关推荐
ZPC82108 分钟前
【无标题】
人工智能·pytorch·算法·机器人
2301_7644413310 分钟前
使用python构建的STAR实验ΛΛ̄自旋关联完整仿真
开发语言·python·算法
Rainy Blue88314 分钟前
前缀和与差分(蓝桥杯高频考点)
数据结构·算法·蓝桥杯
Dfreedom.14 分钟前
机器学习经典算法全景解析与演进脉络(无监督学习篇)
人工智能·学习·算法·机器学习·无监督学习
421!20 分钟前
ESP32学习笔记之GPIO
开发语言·笔记·单片机·嵌入式硬件·学习·算法·fpga开发
智算菩萨28 分钟前
【How Far Are We From AGI】4 AGI的“生理系统“——从算法架构到算力基座的工程革命
论文阅读·人工智能·深度学习·算法·ai·架构·agi
福赖30 分钟前
《算法:生产车间》
算法
空空潍39 分钟前
LeetCode力扣 hot100一刷完结
算法·leetcode
leaves falling42 分钟前
搜索插入位置(第一个≥target的位置)
算法
lcreek42 分钟前
LeetCode 1162.地图分析
算法·leetcode·bfs