C programming string.h function - char *strrchr(const char *str, int c)

The strrchr function in the C programming language is used to find the last occurrence of a character in a string.

The function signature is:

ref‮e‬r to:theitroad.com
char *strrchr(const char *str, int c)

Here, str is a pointer to the null-terminated string to search, and c is the character to find.

The strrchr function returns a pointer to the last occurrence of the character c in the string str, or a null pointer if the character is not found.

For example, consider the following code:

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

int main() {
    char str[] = "Hello, World!";
    char *ptr = strrchr(str, 'l');
    if (ptr != NULL) {
        printf("Found last occurrence of 'l' at position %d\n", ptr - str);
    }
    return 0;
}

This program searches the string "Hello, World!" for the last occurrence of the character 'l'. The strrchr function returns a pointer to the second 'l' character in the string, which is located at position 10. The program then prints the position of the character in the string.