函数原型
char *strstr(const char *haystack, const char *needle)
- haystack:要被检索的字符串(主串);
- needle:出现次数的字符串(子串);
- 返回值:主串中第一次出现子串的位置,如果没出现则为NULL;
代码实例
#include<bits/stdc++.h>
using namespace std;
int main(){
char a[] = "Hello123Hello456Hello789Hello";
char b[] = "Hello";
char *p;
int count = 0;
p = strstr(a,b);
while(p){
count++;
p++;//p++是为了让指针指向下一个字符,否则会出现死循环
p = strstr(p,b);
}
cout<<count;
return 0;
}
输出
4
Comments | NOTHING