Use of strrchr function in C/C++

The strrchr function finds the last occurrence of c (converted to a char) in the string pointed to by s. The terminating null character is also considered to be part of the string and it can be found if searching for ‘\0’.  The strrchr function defines in string.h header file and takes two arguments.

Syntax of strrchr function:

//Syntax of strrchr


char *strrchr(const char *s, int c);

Parameters:

s: This is a pointer to the null-terminated byte string
c: This is a character to search for.

Return:

The strrchr function returns a pointer to the located character, or a null pointer if the character does not occur in the string.

 

Sometimes peoples require to create their own strrchr function for their project. So here in the below section, I shall describe the way to create the strrchr() function or you can say that we will see the implementation of strrchr in C.

Let’s see an example code to understand the functionality of the strrchr in C. In this C code, we will try to find a character in a given character array.

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

int main()
{
    //string
    const char *s = "Aticleworld";

    //char want to search
    char want_search = 'l';

    char *ptr = strrchr(s,want_search);
    if (ptr != NULL)
    {
        printf ("Last occurence of 'l' found at %d.\n", ptr-s+1);
        printf ("search character found:  %s\n", ptr);
    }
    else
    {
        printf ("search character not found\n");
    }

    return 0;
}

When you run the program, the output will be:

Last occurrence of ‘l’ found at 10.
search character found: ld

Explanation:

In the above C program, we are searching character ‘l’ in the given string “Aticleworld”. The strrchr function starts searching from the first character ‘A’. to the null character. When it will find the last ‘l’ it returns the address of character ‘l’.

 

Highlighted points of strrchr() in C:

1.) It takes two parameters and you must include string.h header file in your C program.

2.) The strrchr library function finds the last occurrence of c (converted to a char) in the string pointed to by s.

3.) If the character does not occur in the string, then the strrchr function returns a null pointer.

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

int main()
{
    //string
    const char *s = "Aticleworld";

    //char want to search
    char want_search = 'b';

    char *ptr = strrchr(s,want_search);
    if (ptr != NULL)
    {
        printf ("'b' found at position %d.\n", ptr-s+1);
        printf ("search character found:  %s\n", ptr);
    }
    else
    {
        printf ("search character not found\n");
    }

    return 0;
}

Output:

search character not found

 

How to write your own strrchr() function in C?

Your compiler/standard library will likely have a very efficient and tailored implementation of the strrchr() function. So if not require avoid to create own version of the strrchr function.

Note: Below function only to understand the working of strrchr. There is a lot of scenarios that are not handled in this function. You can handle the scenario as your requirement and if possible then use the standard strrchr function (library function).

char *my_strrchr(const char *s, int c)
{
    char *isCharFind = NULL;
    //handling null pointer
    if(s != NULL)
    {
        do
        {
            if( *s == (char)c )
            {
                isCharFind=s;
            }
        }
        while(*s++);
    }
    return isCharFind;
}

How it works:

The my_strrchr() function takes two arguments one void pointers (void *) for pointing to string and one integer for the character which you want to search in the string.

In this function, we iterating a loop to find the last occurrence of the character in the given string. We are also verifying that s must not be a null pointer.

In the do-while loop, if we find the character, then we are storing the address of the character in a pointer isCharFind and continue the do-while loop till not getting a null character in the string.

if(s != NULL)
{
    do
    {
        if( *s == (char)c )
        {
            isCharFind=s;
        }
    }
    while(*s++);
}

 

Let’s write a small application to test our own created strrchr function,

#include <stdio.h>

char *my_strrchr(const char *s, int c)
{
    char *isCharFind = NULL;
    //handling null pointer
    if(s != NULL)
    {
        do
        {
            if( *s == (char)c )
            {
                isCharFind=s;
            }
        }
        while(*s++);
    }
    return isCharFind;
}

int main()
{
    //string
    const char *s = "Aticleworld";

    //char want to search
    char want_search = 'l';

    //Call own created strrchr
    char *ptr = my_strrchr(s,want_search);
    if (ptr != NULL)
    {
        printf ("Last occurence of 'l' found at %d.\n", ptr-s+1);
        printf ("search character found:  %s\n", ptr);
    }
    else
    {
        printf ("search character not found\n");
    }

    return 0;
}

When you run the program, the output will be:

Last occurrence of ‘l’ found at 10.
search character found: ld

 

Recommended Articles for you:

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *