C语言之strrchr函数

【FROM MSDN && 百科】

原型: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:

 

    1. #include <stdio.h>  
    2. #include <conio.h>  
    3. #include <string.h>  
    4. #pragma warning (disable:4996)  
    5. int main(void)  
    6. {  
    7.     char string[20];  
    8.     char *ptr;  
    9.     char c='r';  
    10.     strcpy(string,"There are two rings");  
    11.     ptr=strrchr(string,c);  
    12.     //ptr=strchr(string,c);  
    13.     if (ptr!=NULL)  
    14.     {  
    15.         printf("The character %c is at position:%s ",c,ptr);  
    16.     }  
    17.     else  
    18.     {  
    19.         printf("The character was not found ");  
    20.     }  
    21.     getch();  
    22.     return 0;