C strrchr library function explanation with example

strrchr C library function :

strrchr() function can be used to get the last substring in a string starting from a specific character. It returns one pointer of a substring. We can use this pointer to print the substring.

Syntax of strrchr() :

The syntax of strrchr() method is as below :

char *strrchr(const char *str, int c)

It takes two arguments :

str: It is the string to search. c: It is the character to search in the string str.

Return value :

The return value of this method is a char * i.e. the pointer to the last occurrence of character c in string str.

Example program :

#include <stdio.h>
#include <string.h>

int main()
{
    char str[] = "the quick brown fox run over the lazy dog";
    char c;

    printf("Enter a character : ");
    scanf("%c", &c);

    char *subStr = strrchr(str, c);
    printf("%s", subStr);

    return 0;
}

Sample Output :

Enter a character : f
fox run over the lazy do

C strrchr example