1,函数名: strstr
功 能: 在串中查找指定字符串的第一次出现
用 法: char *strstr(char *str1, char *str2);
说明:返回指向第一次出现needle位置的指针,如果没找到则返回NULL。
2,实现代码:
功 能: 在串中查找指定字符串的第一次出现
用 法: char *strstr(char *str1, char *str2);
说明:返回指向第一次出现needle位置的指针,如果没找到则返回NULL。
2,实现代码:
#include<iostream>
using namespace std;
char* MyStrStr(char* des, char* t)
{
char* tmp_des;
char* tmp_t;
while(*des)
{
tmp_des = des;
tmp_t = t;
do
{
if (*tmp_t == '\0')
return des;
}
while ( *tmp_des++ == *tmp_t++);
++des;
}
return 0;
}
int main()
{
char *s="Golden Global View";
char *l="Glob";
char *p=NULL;
p=strstr(s,l); //l为空,返回头地址
if(p)
cout<<p<<endl;
else
cout<<"Not Found!"<<endl;
char *p2=NULL;
p2=MyStrStr(s,l); //l为空,返回头地址
if(p2)
cout<<p2<<endl;
else
cout<<"Not Found!"<<endl;
return 0;
}