淡泊明志 宁静致远
分类: 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;
}
chinaunix网友2009-02-16 22:52:17
多谢您的代码分享!
<