strrchr() function in C/C++

The strrchr() function in C/C++ locates the last occurrence of a character in a string. It returns a pointer to the last occurrence in the string. The terminating null character is considered part of the C string. Therefore, it can also be located to retrieve a pointer to the end of a string. It is defined in cstring header file.
Syntax :

const char* strrchr( const char* str, int ch )
            or
char* strrchr( char* str, int ch )

Parameter :The function takes two mandatory parameters which are described below:

  • str : specifies the pointer to the null terminated string to be searched for.
  • ch: specifies the character to be search for.

Return Value: The function returns a pointer to the last location of ch in string, if the ch is found. If not found, it returns a null pointer.



Below programs illustrate the above function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// the strrchr() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // Storing it in string array
    char string[] = "Geeks for Geeks";
  
    // The character we've to search for
    char character = 'k';
  
    // Storing in a pointer ptr
    char* ptr = strrchr(string, character);
  
    // ptr-string gives the index location
    if (ptr)
        cout << "Last position of " << character
             << " in " << string << " is " << ptr - string;
  
    // If the character we're searching is not present in the array
    else
        cout << character << " is not present "
             << string << endl;
    return 0;
}

chevron_right


Output:

Last position of k in Geeks for Geeks is 13

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate 
// the strrchr() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // Storing it in string array
    char string[] = "Geeks for Geeks";
    char* ptr;
  
    // The character we've to search for
    char character = 'z';
  
    // Storing in a pointer ptr
    ptr = strrchr(string, character);
  
    // ptr-string gives the index location
    if (ptr)
        cout << "Last position of " << character
             << " in " << string << " is " << ptr - string;
  
    // If the character we're searching
    // is not present in the array
    else
        cout << character << " is not present in "
             << string << endl;
    return 0;
}

chevron_right


Output:

z is not present in Geeks for Geeks


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.