首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >不是以null结尾的字符串的strstr()

不是以null结尾的字符串的strstr()
EN

Stack Overflow用户
提问于 2011-12-21 11:11:29
回答 4查看 18.7K关注 0票数 31

如何在C中为计数的字符串(即不以null结尾的字符串)执行strstr()就地等效项?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-12-21 13:23:05

如果你害怕O(m*n)行为-基本上,你不需要,这种情况不会自然发生-这是我的一个KMP实现,我已经修改了它,以适应大海桑田的长度。也是一个包装器。如果您想重复搜索,可以编写自己的数组并重用borders数组。

不能保证无bug,但它似乎仍然有效。

代码语言:javascript
复制
int *kmp_borders(char *needle, size_t nlen){
    if (!needle) return NULL;
    int i, j, *borders = malloc((nlen+1)*sizeof(*borders));
    if (!borders) return NULL;
    i = 0;
    j = -1;
    borders[i] = j;
    while((size_t)i < nlen){
        while(j >= 0 && needle[i] != needle[j]){
            j = borders[j];
        }
        ++i;
        ++j;
        borders[i] = j;
    }
    return borders;
}

char *kmp_search(char *haystack, size_t haylen, char *needle, size_t nlen, int *borders){
    size_t max_index = haylen-nlen, i = 0, j = 0;
    while(i <= max_index){
        while(j < nlen && *haystack && needle[j] == *haystack){
            ++j;
            ++haystack;
        }
        if (j == nlen){
            return haystack-nlen;
        }
        if (!(*haystack)){
            return NULL;
        }
        if (j == 0){
            ++haystack;
            ++i;
        } else {
            do{
                i += j - (size_t)borders[j];
                j = borders[j];
            }while(j > 0 && needle[j] != *haystack);
        }
    }
    return NULL;
}

char *sstrnstr(char *haystack, char *needle, size_t haylen){
    if (!haystack || !needle){
        return NULL;
    }
    size_t nlen = strlen(needle);
    if (haylen < nlen){
        return NULL;
    }
    int *borders = kmp_borders(needle, nlen);
    if (!borders){
        return NULL;
    }
    char *match = kmp_search(haystack, haylen, needle, nlen, borders);
    free(borders);
    return match;
}
票数 7
EN

Stack Overflow用户

发布于 2011-12-21 11:22:52

看看下面的函数是否适用于您。我还没有彻底测试它,所以我建议您这样做。

代码语言:javascript
复制
char *sstrstr(char *haystack, char *needle, size_t length)
{
    size_t needle_length = strlen(needle);
    size_t i;
    for (i = 0; i < length; i++) {
        if (i + needle_length > length) {
            return NULL;
        }
        if (strncmp(&haystack[i], needle, needle_length) == 0) {
            return &haystack[i];
        }
    }
    return NULL;
}
票数 8
EN

Stack Overflow用户

发布于 2015-05-05 20:50:33

我偶然发现了这一点,我想分享我的实现。它认为它相当快,我没有任何子调用。

它返回大堆中找到指针的索引,如果没有找到,则返回-1。

代码语言:javascript
复制
/* binary search in memory */
int memsearch(const char *hay, int haysize, const char *needle, int needlesize) {
    int haypos, needlepos;
    haysize -= needlesize;
    for (haypos = 0; haypos <= haysize; haypos++) {
        for (needlepos = 0; needlepos < needlesize; needlepos++) {
            if (hay[haypos + needlepos] != needle[needlepos]) {
                // Next character in haystack.
                break;
            }
        }
        if (needlepos == needlesize) {
            return haypos;
        }
    }
    return -1;
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8584644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档