strrchr ()- 查找字符串中最后出现的字符
格式
#include <string.h>
char *strrchr(const char *string, int c);语言级别
ANSI
线程安全
是
语言环境敏感
此函数的行为可能受当前语言环境的 LC_CTYPE 类别影响。 有关更多信息,请参阅 了解 CCSID 和语言环境。
描述
strrchr() 函数在 string中查找最后出现的 c (转换为字符)。 结束空字符被视为 字符串的一部分。
返回值
strrchr() 函数返回一个指针,该指针指向 string中最后出现的 c 。 如果找不到给定的字符,那么将返回 NULL 指针。
示例
此示例比较了
strchr() 和 strrchr()的使用。 它在字符串中搜索第一个和最后一个出现的 p 。#include <stdio.h>
#include <string.h>
#define SIZE 40
int main(void)
{
char buf[SIZE] = "computer program";
char * ptr;
int ch = 'p';
/* This illustrates strchr */
ptr = strchr( buf, ch );
printf( "The first occurrence of %c in '%s' is '%s'\n", ch, buf, ptr );
/* This illustrates strrchr */
ptr = strrchr( buf, ch );
printf( "The last occurrence of %c in '%s' is '%s'\n", ch, buf, ptr );
}
/***************** Output should be similar to: *****************
The first occurrence of p in 'computer program' is 'puter program'
The last occurrence of p in 'computer program' is 'program'
*/