原型:char *strrchr(const char *str, char c);
#include<string.h>
找一个字符c在另一个字符串str中末次出现的位置(也就是从str的右侧开始查找字符c首次出现的位置)。如果未能找到指定字符,那么函数将返回NULL。
The
strrchr function finds the last occurrence of
c (converted to char) in
str. The search includes the terminating null character.
DEMO:
[cpp] view plaincopy
- #include <stdio.h>
- #include <conio.h>
- #include <string.h>
- #pragma warning (disable:4996)
- int main(void)
- {
- char string[20];
- char *ptr;
- char c=‘r‘;
- strcpy(string,"There are two rings");
- ptr=strrchr(string,c);
- //ptr=strchr(string,c);
- if (ptr!=NULL)
- {
- printf("The character %c is at position:%s\n",c,ptr);
- }
- else
- {
- printf("The character was not found\n");
- }
- getch();
- return 0;
- }
时间: 2024-10-29 19:07:34