c_cpp 在字符串中查找子字符串

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 在字符串中查找子字符串相关的知识,希望对你有一定的参考价值。

int find_substring(const string& s, const string& p) {
    int ns = s.size(), np = p.size();
    if(ns == 0 || np == 0 || ns < np) return -1;
    for(int i=0; i<ns-np; ++i) {
        int j = i, k = 0;
        while(k < np && p[k++] == s[j++]) {}
        if(k < np) continue;
        if(k == np) return i;
    }
    return -1;
}

以上是关于c_cpp 在字符串中查找子字符串的主要内容,如果未能解决你的问题,请参考以下文章