string.h中函数声明如下:
strstr (const char *s1, const char *s2);
注释:
在源字符串s1中查找字符串s2第一次出现的位置,若找到则返回第一次出现的地址,否则返回NULL.
测试代码:
#include<stdio.h>
#include<string.h>
char * mystrstr(const char * ,const char *);
char * mystrstr(const char * src,const char * find){
if(NULL==src || NULL == find)
return NULL;
char * cp = (char *)src;
char * s1, * s2;
while(*cp){
s1 = cp;
s2 = (char *) find;
while(*s1 && *s2 && (*s1 == *s2))
s1++,s2++;
if(!*s2)
return cp;
cp ++;
}
return NULL;
}
int main(){
char * src = "hello, china and hello,world";
char * find = ",";
char * re = mystrstr(src,find);
if(re)
printf("%s\n",re);
else
printf("no find\n");
return 0;
}
char
* mystrstr(
const
char
* ,
const
char
*);
在如下网址中的string.h头文件中看到没有变量名的函数声明。
http://math.ytu.edu.cn/c/function/head-string.html
第一次看到这种函数声明方法没有变量名,如
char
* mystrstr(
const
char
* ,
const
char
*);只指明了变量类型。
参考网址:
http://www.jbox.dk/sanos/source/lib/string.c.html
转载于:https://blog.51cto.com/thinkerfans/1351064