/*********************************************
* @brief 在字符串中查找字符
* @param str 被检索的字符串
* @param ch 要查找的字符
* @return 指向找到的字符的指针
********************************************/
char* strrchr(const char* str, int ch);
!subtitle:说明
在字符串 str 中查找 ch 字符,返回找到的最后一个 ch 字符的地址。
!subtitle:参数
str - 被检索的字符串
ch - 要查找的字符
!subtitle:返回值
指向找到的字符的指针
如果没有找到目标字符,则返回 NULL
#include <stdio.h>
#include <string.h>
int main(void)
{
const char* text = "hello world";
const char* str = strrchr(text, 'l'); // 查找最后一个 'l'
if (str != NULL)
{
printf("地址:%p 偏移:%td 字符:'%c' 剩余字符串:\"%s\"\n", str, str - text, *str, str);
}
return 0;
}
说明:
通过 strrchr 反向查找字母 'l' 的位置。
运行结果:
地址:0x58c6eea42011 偏移:9 字符:'l' 剩余字符串:"ld"
C23 standard (ISO/IEC 9899:2024):
7.24.5.5 The strrchr function (p: TBD)
C17 standard (ISO/IEC 9899:2018):
7.24.5.5 The strrchr function (p: TBD)
C11 standard (ISO/IEC 9899:2011):
7.24.5.5 The strrchr function (p: 368-369)
C99 standard (ISO/IEC 9899:1999):
7.21.5.5 The strrchr function (p: 331)
C89/C90 standard (ISO/IEC 9899:1990):
4.11.5.5 The strrchr function