网站公告列表

  没有公告

加入收藏
设为首页
联系站长
您现在的位置: 网络学院 >> 程序设计 >> VC编程 >> 文章正文
  编写自己的strstr            【字体:
编写自己的strstr
作者:佚名    文章来源:不详    点击数:    更新时间:2007-8-11    
正在装载数据……

前些日子面试,对自己当时写的strstr函数不满意,重新写了一个:

char *strstr_my(const char *src,const char *dest)
{
     const char *pcur = NULL;
     const char *pcmp = dest;
     char a,b;
    
     if(!(a = *src))         //src为空退出
          return NULL;
         
     if(!(b = *pcmp)) return NULL;  //dest为空退出
    
     while(a = *src++)           //src循环
     {
           if(a == b)               //寻找与dest第一个字符相等
           {
                pcur = src;         //用另一个指针跟踪
                pcmp++;
                do
                {
                    a = *pcur++;
                    if(!(b = *pcmp++)) break;                          
                   
                }while(a == b);  
               
                if((!a && !b) || (a && !b)) //如果比较成功退出循环
    {
     pcur = src-1;
                    break;
    }
                  //否则将pcmp,b恢复
                 pcmp = dest;
                 b = *dest;
     pcur = NULL;
           }          
     }  
    
     //如果src有值就寻找成功
     if(pcur != NULL)
        return (char *)pcur;
       
     return NULL;     
}

 

 下面的strstr的源码:

char *
strstr (phaystack, pneedle)
     const char *phaystack;
     const char *pneedle;
{
  const unsigned char *haystack, *needle;
  chartype b;
  const unsigned char *rneedle;

  haystack = (const unsigned char *) phaystack;

  if ((b = *(needle = (const unsigned char *) pneedle)))
    {
      chartype c;
      haystack--;  /* possible ANSI violation */

      {
 chartype a;
 do
   if (!(a = *++haystack))
     goto ret0;
 while (a != b);
      }

      if (!(c = *++needle))
 goto foundneedle;
      ++needle;
      goto jin;

      for (;;)
 {
   {
     chartype a;
     if (0)
     jin:{
  if ((a = *++haystack) == c)
    goto crest;
       }
     else
       a = *++haystack;
     do
       {
  for (; a != b; a = *++haystack)
    {
      if (!a)
        goto ret0;
      if ((a = *++haystack) == b)
        break;
      if (!a)
        goto ret0;
    }
       }
     while ((a = *++haystack) != c);
   }
 crest:
   {
     chartype a;
     {
       const unsigned char *rhaystack;
       if (*(rhaystack = haystack-- + 1) == (a = *(rneedle = needle)))
  do
    {
      if (!a)
        goto foundneedle;
      if (*++rhaystack != (a = *++needle))
        break;
      if (!a)
        goto foundneedle;
    }
  while (*++rhaystack == (a = *++needle));
       needle = rneedle; /* took the register-poor aproach */
     }
     if (!a)
       break;
   }
 }
    }
foundneedle:
  return (char *) haystack;
ret0:
  return 0;




本文来源:http://blog.csdn.net/pSK_LB/archive/2007/08/02/1722851.aspx
站内文章搜索 高级搜索
文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
     directx 图形接口指南(…
     win2k下的api函数的拦截
     用crypto  api  实现公钥…
     根据别人的md5源码封装的…
     vc中使用gdi+合并jpg图片
     document/view的交互 --…
     windows下的函数hook技术
     windows api函数大全一
     用vc 6.0实现串行通信的…
     vc++技术内幕(第四版)…
  • page、request、session、ap…

  • JSF 的性能远不及 JSP 或 St…

  • Struts2学习:在struts2中集…

  • SPRING+STRUTS+HIBERNATE登录…

  • JSP标准模板库(JSTL)入门教…

  • 搭建JSTL运行环境

  • struts多附件上传

  • JavaWeb中的Session、Sessio…

  • struts异常_does not start …

  • boost.any源码重列

  •   网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    网络学院©2007 www.23book.net
    为您提供web编程,vb编程,vc编程,服务器架设管理,数据库设计等方面的知识 站长:David