
- C Library - Home
- C Library - <assert.h>
- C Library - <complex.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <fenv.h>
- C Library - <float.h>
- C Library - <inttypes.h>
- C Library - <iso646.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdalign.h>
- C Library - <stdarg.h>
- C Library - <stdbool.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <tgmath.h>
- C Library - <time.h>
- C Library - <wctype.h>
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
C library - strrchr() function
The C library strrchr() function accepts two argument − searches for the last occurrence of the character c (an unsigned char) in the string pointed to, by the argument str.
Here are some key point which highlights its importance −
- This function is used to set for finding the last occurence of a specified characters.
- The main operation is file path manipulation, parsing URL's and handling the data formats.
- It saves the time and reduce error.
Syntax
Following is the syntax of the C library function strrchr() −
char *strrchr(const char *str, int c)
Parameters
This function takes two parameters −
str − This is the C string.
c − This is the character to be located. It is passed as its int promotion, but it is internally converted back to char.
Return Value
This function returns a pointer to the last occurrence of character in str. If the value is not found, the function returns a null pointer.
Example 1
Following is the basic C library program that shows the usage strrchr() function.
#include <stdio.h> #include <string.h> int main () { int len; const char str[] = "https://www.tutorialspoint.com"; const char ch = '.'; char *ret; ret = strrchr(str, ch); printf("String after |%c| is - |%s|\n", ch, ret); return(0); }
Output
The above code produces the following result −
String after |.| is - |.com|
Example 2
Below the program uses strrchr() to search for a character in a substring of a given string.
#include <stdio.h> #include <string.h> int main() { const char str[] = "Hello, World!"; const char ch = 'W'; // Search in the substring "World!" char* ptr = strrchr(str + 7, ch); if (ptr) { printf("'%c' found at position %ld\n", ch, ptr - str); } else { printf("'%c' not found in the substring\n", ch); } return 0; }
Output
On execution of above code, we get the following result −
'W' found at position 7
Example 3
In this example, we will find the last occurence of a character using strrchr().
#include <stdio.h> #include <string.h> int main() { const char str[] = "Tutorialspoint"; const char ch = 'n'; char* ptr = strrchr(str, ch); if (ptr) { printf("Last occurrence of '%c' in \"%s\" is at index %ld\n", ch, str, ptr - str); } else { printf("'%c' is not present in \"%s\"\n", ch, str); } return 0; }
Output
After executing the code, we get the following result −
Last occurrence of 'n' in "Tutorialspoint" is at index 12