使用函数查找字符串子串

转载自   大佬

1. 考虑用标准函数库中 strstr() 函数

包含文件:string.h
函数名: strstr
函数原型:extern char *strstr(char *str1, char *str2);
功能:从字符串str1中查找是否有字符串str2,如果有,从str1中的str2位置起,返回str1中str2起始位置的指针,如果没有,返回null。
返回值:返回该位置的指针,如找不到,返回空指针


2.使用string的find函数

#include<iostream>  
#include <string>  
using namespace std;  
void main()  
{  
string str1,str2;  
   
cout<<"请输入第一个字符串"<<endl;  
cin>>str1;  
cout<<"请输入子串"<<endl;  
cin>>str2;  
   
int weizhi = 0;  
int count = 0;  
while (1)  
{  
    weizhi = str1.find(str2,weizhi);  
    weizhi++;  
    if (0 == weizhi)  
    {  
        break;  
    }  
    else  
    {  
        count++;  
        cout<<"子串出现的位置是:"<<weizhi<<endl;  
    }  
}  
   
cout<<"子串出现的次数是:"<<count<<endl;  
getchar();  
   
}  



//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相似,只不过是从后向前查找  


原文地址:https://www.cnblogs.com/hjch0708/p/7554817.html