#include<iostream>
using namespace std;
int main()
{
char *strstr(char *src,char *sub);
char p[256];
char q[256];
cout<<"input the src:"<<endl;
cin>>p;
cout<<"input the sub:"<<endl;
cin>>q;
char *r=strstr(p,q);
cout<<"output the ressult:"<<r<<endl;
return 0;
}
char *strstr(char *src,char *sub)
{
if(src==NULL||sub==NULL)
return src;
char *bp=NULL;
char *sp=NULL;
while(*src)
{
bp=src;
sp=sub;
while(*sp++==*bp++)
{
if(!*sp)
return src;
}
/* do
{
if(!*sp)
return src;
}while(*sp++==*bp++);*/
src+=1;
}
return NULL;
}
C++实现字符串中查找子串,也即实现strstr函数
最新推荐文章于 2024-09-27 19:04:50 发布