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

}

相关推荐
毕竟是shy哥1 小时前
计算YOLO数据集中每个类的目标数
算法·yolo·机器学习
M78佐菲1 小时前
Linux学习笔记:TCP协议
linux·笔记·学习·tcp/ip·算法
晊晌_h3 小时前
嵌入式从0到精通——数据结构总结[特殊字符]
数据结构·算法·排序算法
我找到地球的支点啦3 小时前
Matlab系列(009) 一CRC循环冗余校验详解
开发语言·数据结构·算法·matlab·信息与通信
罗西的思考3 小时前
【OpenClaw具身硬件】ZeroClaw 源码阅读笔记(3)--- RAG
人工智能·算法·机器学习
浪里镖客4 小时前
位姿转换矩阵写法-个人习惯(计算机理解其实是相反的)
线性代数·算法·矩阵
小白羊丨7 小时前
如何诊断 Prompt 模板导致的效果下降?
人工智能·算法·prompt
OPEN-F8 小时前
C++11/14新特性精讲:移动语义与智能指针实战
开发语言·c++·算法
lisin-lee-cooper9 小时前
【leetcode658】有序数组找出k个最接近x的数
java·数据结构·算法
sunburn-9 小时前
Java堆(Heap)详解与实战教学
java·开发语言·数据结构·ide·算法