字符串子串的查找


字符串子串的查找

 

// 字符串子串的查找

#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 == '\0') /*如果s2為空,則返回s1*/
        return ((char *)s1);
    for (; s1 != '\0'; ++s1) /*每次后移s1的位置,在新的位置進行下一次匹配*/
    {
        const char *sc1, *sc2;
        while ((*s1 != *s2) && (*s1 != '\0')) ++s1; /*在s1中找到和s2第一個字符匹配的位置*/
        if (*s1 == '\0') /*如果找不到,說明s1現在的位置不匹配,退出循環進行下一次匹配*/
            break;
        else /*如果找到和s2第一個字符匹配的位置,開始逐個匹配s2后面的字符*/
            for (sc1 = s1, sc2 = s2; sc1 != '\0'; ++sc1, ++sc2)
            {
                if (*sc2 == '\0') /*如果匹配完畢,返回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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM