C/C++库函数strstr和find实现子字符串查找

一、循环的使用和vector的用处

循环有两种,for循环和while循环,前者适合循环条件和次数已知的情形,后者适合循环条件和次数未知的情形,两者的效率相当。vector适合数组的大小未知的情形。

二、string.find()函数用法

1.返回字符串s1在s中的位置,如果没有找到,则返回-1
position=s.find(s1);
2.返回任意字符s1在s中第一次出现的位置,s1为字符,不能为字符串 ‘a’ “a”都可以
position=s.find_first_of(s1);
3.从字符串s下标为a开始查找字符串s1,返回起始位置 s.find(s1,a); 查找不到返回-1
position=s.find(s1,3);
4.查找字符串s1与s第一个不匹配的位置

#include <iostream>  
#include <string.h>  
using namespace std;  

int main()  
{  
   string s="aaabcd";  
   string s1="aaafcd";  
   int position;  
   position=s.find_first_not_of(s1);  
   cout<<position<<endl;  
   return 0;  
} 

三、库函数strstr和find实现子字符串查找

#include<iostream>
#include<string>
#include<cstring>
using namespace std;

int main()
{
    char a[] = "abcddabc";
    char b[] = "dda";
    int j;
    string str1(a);
    string str2(b);
    //方法一
    int i = str1.find(str2);  //返回即子字符串索引3
    //方法二
    char *rel = strstr(a, b); //首次出现地址,strstr保存的是ddabc
    if (rel != NULL)
        j = rel -a;         //根据返回子字符串匹配结果输出索引位

    return 0;
}

注意两个函数返回值的区别!

四、程序代码编写

#include <iostream>
#include <vector>
using namespace std;

//函数1: 查找子串sub在str中出现的次数--用C++方式--子串可重叠 
int fun1(const std::string& str, const std::string& sub)
{
    int num = 0;
    size_t len = sub.length();
    for (size_t i = 0; (i = str.find(sub, i)) != std::string::npos; num++, i++);
    return num;
}
//函数2: 查找子串sub在str中出现的次数--用C方式--子串可重叠 
int fun2(const char* str, const char* sub)
{
    int num = 0;
    for (const char* pstr = str; *pstr && (pstr = strstr(pstr, sub)); pstr++, num++);
    return num;
}
//函数3: 查找子串sub在str中出现的次数--用C++方式--子串不重叠  
int fun3(const std::string& str, const std::string& sub)
{
    int num = 0;
    size_t len = sub.length();
    if (len == 0)len = 1;//应付空子串调用
    for (size_t i = 0; (i = str.find(sub, i)) != std::string::npos; num++, i += len);
    return num;
}
//函数4: 查找子串sub在str中出现的次数--用C方式--子串不重叠 
int fun4(const char* str, const char* sub)
{
    int num = 0;
    size_t len = strlen(sub);
    if (len == 0)len = 1;//应付空子串调用
    for (const char* pstr = str; *pstr && (pstr = strstr(pstr, sub)); pstr += len, num++);
    return num;
}
void main()
{
    string str("AAbbAAbbAA");
    string sub("AA");
    int index=0,num = 0;
    vector<int>indexsum;                     //个数不确定就是用vector数组
    while (1){                               //循环次数不确定就用while循环
        index = str.find(sub,index);         //深刻理解find函数的用法,没有找到是返回-1,可以从索引处开始查找
        if (index == -1){
            break;
        }
        else{
            num++;
            indexsum.push_back(index);      //存储索引要用vector
            index += sub.length();
        }
    }
    if (num == 0){
        cout << "not found!";
    }
    else{
        cout << "总共发现字符串" << num << "次" << endl;
        cout << "索引分别为: "<< endl;
        for (int i = 0; i<num; i++){          //num已知所以可以用for循环
            cout << indexsum[i] << endl;
        }
    };

    cout << fun1(str, sub) << endl;          
    cout << fun2(str.c_str(), sub.c_str()) << endl;   //C语言没有string类型,必须把他转成字符指针
    cout << fun3(str, sub) << endl;
    cout << fun4(str.c_str(), sub.c_str()) << endl;


}

猜你喜欢

转载自blog.csdn.net/menshu1892/article/details/79920456