#include <string.h>
char *strrchr( const char *str, int ch );
函数返回一个指针,它指向字符 ch 在字符串 str 末次出现的位置,如果匹配失败,返回NULL。
#include <string.h> #include <stdio.h> int main(void) { char string[15]; char *ptr, c = 'r'; strcpy(string, "This is a string"); ptr = strrchr(string, c); if (ptr) { printf("The character %c is at position: %d\n", c, ptr-string); } else { printf("The character was not found\n"); } return 0; }
输出结果:
The character r is at position: 12