#include <iostream>
#include <cstring>
using namespace std;
const char * my_strstr(const char *s1,const char *s2)
{
const char *bp;
const char *sp;
if( s1 == NULL || s2 == NULL)
{
return NULL;
}
while(*s1)
{
bp = s1;
sp = s2;
//*号和++是单目运算符,优先级相同,但是结合方向是从右至左。
//就是把*号右面的看成一个整体,也就是*(a++)=*(b++); 因为++在后,所以最后做增1,先做*(a)=*(b), 然后b++, a++;
while(*bp++ == *sp++)//*bp *sp -> *bp == *sp ,a++,b++;
{
if(!*sp)//sp = 0;
{
return s1;
}
}
s1++;
}
return NULL;
/*
while(*s1 != '\0')
{
if(strncmp(s1,s2,len) == 0)
{
return int(s1);
}
s1++;
}
*/
return 0;
}
int main()
{
char str[100];
char ptr[100];
cout << "Please input str and str" << endl;
cin.getline(str,100);
cin.getline(ptr,100);
const char *ret = my_strstr(str,ptr);
if(ret == 0)
{
cout <<"No found" << endl;
}
else
{
cout << "ret = " << (int *)ret << endl;
cout << "Found" << endl;
}
return 0;
}
实现找子串的编程
最新推荐文章于 2023-02-26 17:29:37 发布