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

}

相关推荐
2301_822703201 天前
Flutter 框架跨平台鸿蒙开发 - 创意声音合成器应用
算法·flutter·华为·harmonyos·鸿蒙
cmpxr_1 天前
【C】数组名、函数名的特殊
c语言·算法
KAU的云实验台1 天前
【算法精解】AIR期刊算法IAGWO:引入速度概念与逆多元二次权重,可应对高维/工程问题(附Matlab源码)
开发语言·算法·matlab
会编程的土豆1 天前
【数据结构与算法】再次全面了解LCS底层
开发语言·数据结构·c++·算法
大熊背1 天前
如何利用Lv值实现三级降帧
算法·自动曝光·lv·isppipeline
大尚来也1 天前
驾驭并发:.NET多线程编程的挑战与破局之道
java·前端·算法
向阳而生,一路生花1 天前
深入浅出 JDK7 HashMap 源码分析
算法·哈希算法
君义_noip1 天前
信息学奥赛一本通 4150:【GESP2509七级】⾦币收集 | 洛谷 P14078 [GESP202509 七级] 金币收集
c++·算法·gesp·信息学奥赛·csp-s
摸个小yu1 天前
【力扣LeetCode热题h100】链表、二叉树
算法·leetcode·链表
汀、人工智能1 天前
[特殊字符] 第93课:太平洋大西洋水流问题
数据结构·算法·数据库架构·图论·bfs·太平洋大西洋水流问题