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

全部博文(60)

文章存档

2011年(60)

我的朋友

分类: C/C++

2011-01-02 10:54:55

    strchr 库函数用于在字符串中查找第一次出现指定字符的子字符串。

  1. #include <stdio.h>

  2. char *strchr_test(const char *s, int c);

  3. int
  4. main(int argc, char **argv)
  5. {
  6.     char *s = "hello andy";
  7.     char c = 'l';
  8.     
  9.     printf("Result: %s\n", strchr_test(s,c));

  10.     return 0;
  11. }

  12. char*
  13. strchr_test(const char *s, int c)
  14. {
  15.     for (; *s != (char)c; ++s) {
  16.         if (*s == '\0') {
  17.             return NULL;
  18.         }
  19.     }

  20.     return (char *)s;
  21. }
注:参数为int, 因为EOF比char的范围大。
阅读(289) | 评论(0) | 转发(0) |
0

上一篇:strcat

下一篇:strspn

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