strstr函数的用法
包含文件:string.h
函数名: strstr
函数原型:
extern char *strstr(char *str1, const char *str2);
语法:
* strstr(str1,str2)
str1: 被查找目标 string expression to search.
str2: 要查找对象 The string expression to find.
返回值:若str2是str1的子串,则返回str2在str1的首次出现的地址;如果str2不是str1的子串,则返回NULL。
应用举例
#include <syslib.h>#include <string.h>main(){ char *s="GoldenGlobalView"; char *l="lob"; char *p; clrscr(); p=strstr(s,l); if(p) printf("%s",p); else printf("NotFound!"); getchar(); return0;}//功能:从字串” string1 onexxx string2 oneyyy”中寻找”yyy”
(假设xxx和yyy都是一个未知的字串)
char *s=”string1onexxxstring2oneyyy”;char *p;p=strstr(s,”yyy”);if(p!=NULL) printf(“%s”,p);else printf("notfound\n");说明:如果直接写语句p=strstr(s,”one”),找到的是onexxxstring2oneyyy
char *mystrstr(char*s1,char*s2){ if(*s1==0) { if(*s2) return (char*)NULL; return (char*)s1; } while(*s1) { int i=0; while(1) { if(s2[i]==0) return s1; if(s2[i]!=s1[i]) break; i++; } s1++; } return (char*)NULL;}
浙公网安备 33010602011771号