码迷,mamicode.com
首页 > 编程语言 > 详细

[C++]strstr内部实现的不同版本

时间:2014-08-24 18:00:42      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   strong   for   ar   div   log   amp   

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  }

 

 

 

[C++]strstr内部实现的不同版本

标签:style   blog   color   strong   for   ar   div   log   amp   

原文地址:http://www.cnblogs.com/panweishadow/p/3932990.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!