1. 编程学习网 > 编程教程 > C语言教程 > C语言经典案例:计算字符串中子串出现的次数 。

C语言经典案例:计算字符串中子串出现的次数 。

需求描写:盘算字符串中子串涌现的次数 。

C语言案例剖析:无。

实现代码如下:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

int main()

{

int i,j,k,TLen,PLen,count=0;

char T[50],P[10];

printf("请输出两个字符串,以回车离隔,母串在前,子串在后:\n");

gets(T);

gets(P);

TLen=strlen(T);

PLen=strlen(P);

for(i=0;i<=TLen-PLen;i++)

{

for(j=0,k=i;j<PLen&&P[j]==T[k];j++,k++)

;

if(j==PLen)count++;

}

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

system("pause");

return 0;

}

以上实例运行输入结果为:

请输出两个字符串,以回车离隔,母串在前,子串在后:

abca

a

2

本文由IT教学网整理发布,转载请注明出处:http://www.itjx.com/jiaocheng/cyuyan/971.html

联系我们

在线咨询:点击这里给我发消息

咨询电话:400-998-2681

工作时间:7*24小时无休