求字符串中的某个子串重复次数

#include<stdio.h>


int count_substr(char *str, char *substr)
{
    int count = 0;
    char *p = substr;
    
    while(*str != '\0')
    {
        while( *p != '\0')
        {
            if( *(str++) != *(p++) )
            {
                if( *(str-1) == substr[0] && *str == substr[1])
                    str = str - 1;        
                break;
            }
            
            if ( *p == '\0') 
                count = count + 1;
        }
        
        p = substr;
    }
    return count;
}

int main()
{
    char *str = "wdxdsdfasdfxwdddxddwddxwdxxxx";
    char *substr = "wdx";
    int i = count_substr(str, substr);
    printf("%d\n",i);

}

 

posted @ 2016-10-10 20:46  小德cyj  阅读(256)  评论(0编辑  收藏  举报