您的位置:首页 > 其它

输入一个字符串,计算字符串中子串出现的次数

2017-02-02 20:17 561 查看
#include <stdio.h>

#include <string.h>

int time(char *s1,char*s2);

int main()

{

        char str[256];

        char son[25];

        int t;

        printf("Please input a long strings:");

        scanf("%s",str);

        printf("Please input a son strings:");

        scanf("%s",son);

        t = time(str,son);

        printf("The son strings total show %d times in long strings.\n",t);

       return 0;

}

int time(char *s1,char*s2)

{

        char *p1 = s1,*p2 = s2;

        int count = 0,k,len;

        len = strlen(s2);

        while( (*p1) != '\0' )

        {

                k = 0;

        while( (*p1) == (*p2) && (*p2) != '\0' )           //查找字符串中的子串

        {

                       p1++;

                       p2++;

                       k++;

        }

                if( k == len )

                        count++;

                p2 = s2;

                p1++;

        }

        return count;

}



程序运行示例:

Please input a long strings:zxcvbnzxcvzxcvbnbvczxcvzxcvbn

Please input a son strings:zxc

The son strings total show 5 times in long strings.




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐