函数
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);
}