Chinaunix首页 | 论坛 | 博客
  • 博客访问: 87340
  • 博文数量: 60
  • 博客积分: 4002
  • 博客等级: 中校
  • 技术积分: 645
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-17 18:11
文章分类

全部博文(60)

文章存档

2011年(60)

我的朋友

分类: C/C++

2011-01-02 12:39:32

   strstr 库函数用于找子串。

  1. #include <stdio.h>
  2. #include <string.h>

  3. char *strstr_test(const char *s1, const char *s2);

  4. int
  5. main(int argc, char **argv)
  6. {
  7.     char *s1 = "hello andy haha";
  8.     char *s2 = "andy";

  9.     printf("Result: %s\n", strstr_test(s1, s2));

  10.     return 0;
  11. }

  12. char *
  13. strstr_test(const char *s1, const char *s2)
  14. {
  15.     size_t l1, l2;

  16.     l2 = strlen(s2);
  17.     if (!l2) {
  18.         return (char *)s1;
  19.     }
  20.     l1 = strlen(s1);
  21.     
  22.     while (l1 >= l2) {
  23.         l1--;
  24.         if (!memcmp(s1, s2, l2)) {
  25.             return (char *)s1;
  26.         }
  27.         s1++;
  28.     }
  29.     
  30.     return NULL;
  31. }

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

上一篇:strsep

下一篇:mem

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