1.1.Copyright 1990 Software Development Systems, Inc. 1 char*strstr(constchar*s1,constchar*s2) 2 { 3 intlen2; 4 if(!(len2=strlen(s2)))//此种情况下s2不能指向空,否则strlen无法测出长度,这条语句错误 5 return(char*)s1; 6 for(;*s1;++s1) 7 { 8 if(*s1==*s2&&strncmp(s1,s2,len2)==0) 9 return(char*)s1; 10 } 11 returnNULL; 12 } 2.Copyright 1986 - 1999 IAR Systems. All rights reserved
1 char*strstr(constchar*s1,constchar*s2) 2 { 3 intn; 4 if(*s2) 5 { 6 while(*s1) 7 { 8 for(n=0;*(s1+n)==*(s2+n);n++) 9 { 10 if(!*(s2+n+1)) 11 return(char*)s1; 12 } 13 s1++; 14 } 15 returnNULL; 16 } 17 else 18 return(char*)s1; 19 }
3.GCC-4.8.0
1 char* 2 strstr(constchar*s1,constchar*s2) 3 { 4 constchar*p=s1; 5 constsize_tlen=strlen(s2); 6 for(;(p=strchr(p,*s2))!=0;p++) 7 { 8 if(strncmp(p,s2,len)==0) 9 return(char*)p; 10 } 11 return(0); 12 }
时间: 2024-11-05 20:29:20