C 练习实例96

C语言教程评论

C 练习实例96

题目:计算字符串中子串出现的次数 。

程序分析:无。文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili96.html

实例

#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;
}

以上实例运行输出结果为:文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili96.html

请输入两个字符串,以回车隔开,母串在前,子串在后:
abca
a
2
文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili96.html文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili96.html
运营不易,
感谢支持!
weinxin
我的微信
我的微信公众号
我的微信公众号扫一扫
weinxin
我的公众号
 最后更新:2021-12-7
公式库网
  • 本文由 公式库网 发表于 2021年12月6日21:35:41
  • 转载请务必保留本文链接:https://www.gongshiku.com/html/202112/c-lianxishili96.html

发表评论