C/C++ 实现strrchr函数

函数

char *strrchr(const char *str, int c) 

在参数 str 所指向的字符串中搜索最后一次出现字符 c(一个无符号字符)的位置。

参数

  • str -- C 字符串。
  • c -- 要搜索的字符。以 int 形式传递,但是最终会转换回 char 形式。

返回值

该函数返回 str 中最后一次出现字符 c 的位置。如果未找到该值,则函数返回NULL。

源码

/***
*char *strrchr(string, ch) - find last occurrence of ch in string
*
*Purpose:
*       Finds the last occurrence of ch in string.  The terminating
*       null character is used as part of the search.
*
*Entry:
*       char *string - string to search in
*       char ch - character to search for
*
*Exit:
*       returns a pointer to the last occurrence of ch in the given
*       string
*       returns NULL if ch does not occur in the string
*
*Exceptions:
*
*******************************************************************************/

char *strrchr (const char * string, int ch)
{
	char *start = (char *)string;

	while (*string++)                       /* find end of string */
			;
											/* search towards front */
	while (--string != start && *string != (char)ch)
			;

	if (*string == (char)ch)                /* char found ? */
			return( (char *)string );

	return(NULL);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值