原型:char *strstr(const char *str1, const char *str2);
#include<string.h>
找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)。返回该位置的指针,如找不到,返回空指针。
Returns a pointer to the first occurrence of strSearch in str, or NULL if strSearch does not appear in str. IfstrSearch points to a string of zero length, the function returns str.
另一个实现:
又一个实现:
#include <iostream>
#include <string>
using namespace std;
//c语言实现strstr
const char* isSub(const char* str, const char *subs){
//特殊情况
if(!*subs)
return str;
const char* tmp=str;
while (*tmp!=' ')
{
//用于每次将父串向后移动一个字符
const char* tmp1=tmp;
//记录子串地址
const char* sub1=subs;
while (*sub1!=' '&&*tmp1!=' ')
{
//若不相等则跳出,将父串后移一个字符
if (*sub1!=*tmp1)
break;
//若相等且子串下一个字符是末尾则是这个父串的子串
if (*sub1==*tmp1&&*(sub1+1)==' ')
return tmp;
//若相等则继续比较下一个字符
if (*sub1==*tmp1)
{
sub1++;
tmp1++;
}
}
tmp++;
}
return NULL;
}
int main(){
char* str1="ababcdddb";
char* str="";
const char *res=isSub(str1,str);
if (res!=NULL)
{
cout << res << endl;
}
else
cout << "null" << endl;
//cout << isSub(str1,str) << endl;
return 0;
}