strrstr() function in C Programming Language

Rumman Ansari   Software Engineer   2019-03-31   9615 Share
☰ Table of Contents

Table of Content:


strrstr() function returns pointer to the last occurrence of the string in a given string. Syntax for strrstr( ) function is given below.

Syntax

char *strrstr(const char *str1, const char *str2);

Important Points

strrstr() function is non standard function which may not available in standard library in C.

Program

In the below program, strrstr() function is used to locate last occurrence of the string "test" in the string "This is a test string for testing". Pointer is returned at last occurrence of the string “test”.


#include<stdio.h>
#include<string.h>
int main ()
{
  char string[55] ="This is a test string for testing";
  char *p;
  p = strrstr (string,"test");
  if(p)
  {
    printf("string found\n" );
    printf ("Last occurrence of string \"test\" in \"%s\" is"\
           " \"%s\"",string, p);
  }
  else printf("string not found\n" );
   return 0;
}

Output

string found
Last occurrence of string “test” in “This is a test string for testing” is “testing”