Chinaunix首页 | 论坛 | 博客
  • 博客访问: 13246
  • 博文数量: 1
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2015-05-22 09:36
文章分类
文章存档

2016年(1)

我的朋友
最近访客

分类: C/C++

2016-06-01 14:11:30

原文地址:strstr函数的实现 作者:qiyuefeng11

函数要求:使用标准C语言实现下列标准库函数,设计中不得使用其他库函数。
函数原型:char *strstr(char *str1,char *str2);
说明:在字符串str1中,寻找字串str2,若找到返回找到的位置,否则返回NULL。

实现代码如下:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <assert.h>
  4. char *strstr(const char *find,const char *need);
  5. int main()
  6. {
  7.     const char buf1[100] = "heelor";
  8.     const char buf2[100] = "lor";
  9.     
  10.     const char *res =strstr(buf1,buf2);
  11.     printf("the res=%s",res);
  12.     return 0;


  13. }

  14. char *strstr(const char *find, const char *need)
  15. {
  16.     assert(find !=NULL && need !=NULL);

  17.     while( *find !='\0')
  18.     {
  19.     const char *p=find;
  20.     const char *q =need;
  21.     const char *res= NULL;


  22.     if(*p == *q)
  23.         {    
  24.         res= p;
  25.         while(*p++ == *q++)
  26.             ;
  27.         if(*q == '\0')
  28.         return res;

  29.     
  30.         }
  31.     find ++;
  32.     }
  33.     return NULL;

  34. }


阅读(1840) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:没有了

给主人留下些什么吧!~~