C 练习实例96
题目:计算字符串中子串出现的次数 。
程序分析:无。
实例
// Created by study.p2hp.com on 15/11/9.
// Copyright © 2015年 高手教程. All rights reserved.
//
#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
C 语言经典100例



杜若隰子
250***7597@qq.com
参考地址
参考方法:
#include<stdio.h> #include<string.h> int fun(char *a,char *b) { int count = 0; char *p; int len_b = strlen(b); while((p = strstr(a,b)) != NULL) { count++; a = p + len_b; } return count; } int main() { printf("%d\n",fun("dh123dhgfdgsshdhscnbdh","dh")); return 0; }杜若隰子
250***7597@qq.com
参考地址