查找源字符串中是否含有子串

#include <stdio.h>
#include <string.h>

int seek_substr(char *src, char *sub)
{
    char *p = src, *q = sub;
    for(; *(p+strlen(sub)-1); p++)
    {
        for(q=sub; (*q == *p)&&(*q); q++,p++);
        if('\0' == *q) //It contains substring.
            return 1;
    }
    return 0;
}

int main(void)
{
    char *src = "hel lo123world1245nihao12";
    char *sub = "123";
    int flag = seek_substr(src, sub);
    if(1 == flag)
        printf("%s is in %s!\n", sub, src);
    else
        printf("%s is not in %s!\n", sub, src);

    return 0;
}
阅读更多

没有更多推荐了,返回首页