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 in 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.
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.
Given a string in C++, we need to find the last occurrence of a character, lets say ‘a’.
Input : string = 'This is a string' Output :9 Input :string = 'My name is Ayush' Output :12
1. Pass the given string in the strchr() function and mention the character you need to point.
3. The function return a value,print the value.
C Examples :
String after last e is : eks String after last m is : (null)
Practical Application : Since it returns the entire string after 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.
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 contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- strrchr() function in C/C++
- What happens when a virtual function is called inside a non-virtual function in C++
- Function Overloading vs Function Overriding in C++
- Difference between Virtual function and Pure virtual function in C++
- How to call function within function in C or C++
- Difference between virtual function and inline function in C++
- Write a one line C function to round floating point numbers
- Does C support function overloading?
- How can I return multiple values from a function?
- What is the purpose of a function prototype?
- How to declare a pointer to a function?
- C function to Swap strings
- Can we call an undeclared function in C++?
- What is evaluation order of function parameters in C?
- Can we use function on left side of an expression in C and C++?
- Declare a C/C++ function returning pointer to array of integer pointers
- Function overloading and return type
- Function Pointer in C
- Importance of function prototype in C
- What happens when a function is called before its declaration in C?
Improved By : AndroidOcean