Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1396133
  • 博文数量: 143
  • 博客积分: 10005
  • 博客等级: 上将
  • 技术积分: 1535
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-23 17:25
个人简介

淡泊明志 宁静致远

文章分类

全部博文(143)

文章存档

2011年(2)

2009年(1)

2007年(22)

2006年(118)

我的朋友

分类: C/C++

2006-11-25 16:05:07

C语言库函数源代码】

【本程序在Dev C++ 4.9.9.2 下编译通过】

/*

   Finds the last occurrence of ch in string.  The terminating null character is used as part of the search.

   查找在字符串中最后一次出现字符’ch’的位置。如果str中存在字符ch,返回出现ch的位置的指针;否则返回NULL。

*/

#include

char * my_strrchr(const char * str,int ch)

{

   char *p = (char *)str;

   while (*str) str++;

   while (str-- != p && *str != (char)ch);

   if (*str == (char)ch)

        return( (char *)str );

   return(NULL);

}

int main()

{

   char *str = "ammana_babi";

   char * p;

   char ch;

  

   ch = '9';

   p = (char *)my_strrchr(str,ch);

   if(p == NULL)

      printf("Can't find the character %c !\n",ch);

   else

      printf("Find the character %c !\n",*p);

  

   ch = 'b';

   p = (char *)my_strrchr(str,ch);

   if(p == NULL)

      printf("Can't find the character %c !\n",ch);

   else

      printf("Find the character %c !\n",*p);

   system("pause");

   return 0;

}

阅读(5312) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-02-16 22:52:17

多谢您的代码分享! <>里关于strrchr() 提到 "if c is the null character (0), the functions will return the position of the terminating null character of s". 使用以下代码, char *s = ""; char ch = '\0'; char *p = my_strrchr(s, ch); 可发现p指向的地址是s的前一位。这里正确情况是p与s相等。