字符串子串的查找

字符串子串的查找

// 字符串子串的查找

#include <iostream>
#include <string>

using namespace std;

/*
//string类的查找函数:
int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置
int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
//查找成功时返回所在位置,失败返回string::npos的值
int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int pos, int n = npos) const;
int rfind(const string &s,int pos = npos) const;
//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值
int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置
int find_first_of(const char *s, int pos = 0) const;
int find_first_of(const char *s, int pos, int n) const;
int find_first_of(const string &s,int pos = 0) const;
//从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos
int find_first_not_of(char c, int pos = 0) const;
int find_first_not_of(const char *s, int pos = 0) const;
int find_first_not_of(const char *s, int pos,int n) const;
int find_first_not_of(const string &s,int pos = 0) const;
//从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos
int find_last_of(char c, int pos = npos) const;
int find_last_of(const char *s, int pos = npos) const;
int find_last_of(const char *s, int pos, int n = npos) const;
int find_last_of(const string &s,int pos = npos) const;
int find_last_not_of(char c, int pos = npos) const;
int find_last_not_of(const char *s, int pos = npos) const;
int find_last_not_of(const char *s, int pos, int n) const;
int find_last_not_of(const string &s,int pos = npos) const;
//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找
*/
void calcSubStr(const string & str1, const string & str2)
{
    size_t weizhi = 0;
    int count = 0;
    while (true)
    {
        weizhi = str1.find(str2, weizhi);
        weizhi++;
        if (0 == weizhi)
        {
            break;
        }
        else
        {
            count++;
            cout << "子串出现的位置是: " << weizhi << endl;
        }
    }
    cout << "子串出现的次数是: " << count << endl;
}

/*
C语言库函数用于在字符串中查找子串。函数原型为char *(strstr)(const char *s1, const char *s2)
函数的参数是两个字符串,函数返回s2在s1中第一次出现的位置(字符指针)。如果在s1中没有找到s2,返回空。
如果s2为空,则返回s1。
*/
char* my_strstr(const char *s1, const char *s2)
{
    if (*s2 == '') /*如果s2为空,则返回s1*/
        return ((char *)s1);
    for (; s1 != ''; ++s1) /*每次后移s1的位置,在新的位置进行下一次匹配*/
    {
        const char *sc1, *sc2;
        while ((*s1 != *s2) && (*s1 != '')) ++s1; /*在s1中找到和s2第一个字符匹配的位置*/
        if (*s1 == '') /*如果找不到,说明s1现在的位置不匹配,退出循环进行下一次匹配*/
            break;
        else /*如果找到和s2第一个字符匹配的位置,开始逐个匹配s2后面的字符*/
            for (sc1 = s1, sc2 = s2; sc1 != ''; ++sc1, ++sc2)
            {
                if (*sc2 == '') /*如果匹配完毕,返回s1此时的位置*/
                    return ((char *)s1);
                else if (*sc1 != *sc2) /*如果后面有一个字符不匹配,说明s1现在的位置不匹配,退出循环进行下一次匹配*/
                    break;
            }
    }
    return (NULL);
}

int main(int argc, char * argv[])
{
    string str1, str2;
    cout << "请输入第一个字符串" << endl;
    cin >> str1;
    cout << "你输入的第一个字符串为: " << str1 << endl;
    cout << "请输入子串" << endl;
    cin >> str2;
    cout << "你输入的子串为: " << str2 << endl;

    calcSubStr(str1, str2);

    // 从父串中查找子串, 可以使用专门的KMP算法, 效率很好。

    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/lsgxeva/p/7953690.html