C:countOfSubstring 查找子串出现次数

//查找子串出现次数(15分)
// 返回字符串s中出现子串substring的次数
// jsdkabcejifabcfjeabckjef  abc
size_t countOfSubstring(const char * s, const char * sub)
{
    if (s==NULL||sub==NULL) {
        return -1;
    }
    
    int count=0;
    const char *sub_temp=sub;
    
    while (*s!='\0') {
        while (1) {
            if (*s++!=*sub++) {
                break;
            }
            if (*sub=='\0') {
                count++;
                s--;
            }
        }
        
        sub=sub_temp;
    }
    
    return count ;
}

猜你喜欢

转载自blog.csdn.net/hurricane111/article/details/40553659