strrchr() function in C/C++ - GeeksforGeeks

12 Oct, 2021
strrchr() function In C++, strrchr() is a predefined function used for string handling. cstring is the header file required for string functions.This function returns a pointer to the last occurrence of a character in a string. The character whose last occurrence we want to find is passed as the second argument to the function and the string in which we have to find the character is passed as the first argument to the function. Syntax  
char *strrchr(const char *str, int c)
Here, str is the string and c is the character to be located. It is passed as its int promotion, but it is internally converted back to char. Application Given a string in C++, we need to find the last occurrence of a character, let’s say ‘a’.Examples:  
Input : string = 'This is a string'
Output :9

Input :string = 'My name is Ayush'
Output :12
Algorithm 1. Pass the given string in the strchr() function and mention the character you need to point to. 2. The function returns a value, print the value. 
 
CPP
// C++ program to demonstrate working strchr()#include <iostream>#include <cstring>using namespace std; int main(){  char str[] = "This is a string";  char * ch = strrchr(str,'a');  cout << ch - str + 1;  return 0;}
Output:  
9
C Examples :  
C
// C code to demonstrate the working of// strrchr() #include <stdio.h>#include <string.h> // Driver functionint main(){     // initializing variables    char st[] = "GeeksforGeeks";    char ch = 'e';    char* val;     // Use of strrchr()    // returns "ks"    val = strrchr(st, ch);     printf("String after last %c is :  %s \n", ch, val);     char ch2 = 'm';     // Use of strrchr()    // returns null    // test for null    val = strrchr(st, ch2);     printf("String after last %c is :  %s ", ch2, val);     return (0);}
Output:  
String after last e is :  eks 
String after last m is :  (null)
Practical Application: Since it returns the entire string after the last occurrence of a particular character, it can be used to extract the suffix of a string. For e.g to know the entire leading zeroes in a denomination when we know the first number. This example is demonstrated below. 
C
// C code to demonstrate the application of// strrchr() #include <stdio.h>#include <string.h> // Driver functionint main(){     // initializing the denomination    char denom[] = "Rs 10000000";     // Printing original string    printf("The original string is : %s", denom);     // initializing the initial number    char first = '1';    char* entire;     // Use of strrchr()    // returns entire number    entire = strrchr(denom, first);     printf("\nThe denomination value is : %s ", entire);     return (0);}
Output:  
The original string is : Rs 10000000
The denomination value is : 10000000
This article is contributed by Ayush Saxena and Vaishnavi Tripathi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
decpk
biyaniram99
niharikatanwar61
C-Library
C-String
CPP-Library
cpp-string
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
std::sort() in C++ STL
Bitwise Operators in C/C++
rand() and srand() in C/C++
Left Shift and Right Shift Operators in C/C++
Vector in C++ STL
Initialize a vector in C++ (6 different ways)
std::sort() in C++ STL
Bitwise Operators in C/C++
Socket Programming in C/C++