w3resource

C - strrchr() function

C strrchr() function - String scanning operation

Syntax:

char *strrchr(const char *str, int c)

The strrchr() function is used to find the last occurrence of c (converted to a character) in string. The ending null character is considered part of the string.

Parameters:

Name Description Required /Optional
str Null-terminated string to search. Required
c Character to be located. Required

Return value from strrchr()

  • Returns a pointer to the last occurrence of c in str.
  • NULL if c isn't found.

Example: strrchr() function.


#include <stdio.h>
#include <string.h> 
#define SIZE 40
 
int main(void)
{
  char text[SIZE] = "C Programming";
  char * ptr;
  int ch = 'C';
  ptr = strrchr( text, ch );
  printf("The last occurrence of %c in '%s' is '%s'\n", ch, text, ptr);
  ch = 'P';
  ptr = strrchr( text, ch );
  printf("The last occurrence of %c in '%s' is '%s'\n", ch, text, ptr);
  ch = 'i';
  ptr = strrchr( text, ch );
  printf("The last occurrence of %c in '%s' is '%s'\n", ch, text, ptr);
  ch = 'a';
  ptr = strrchr( text, ch );
  printf("The last occurrence of %c in '%s' is '%s'\n", ch, text, ptr);
}

Output:

The last occurrence of C in 'C Programming' is 'C Programming'
The last occurrence of P in 'C Programming' is 'Programming'
The last occurrence of i in 'C Programming' is 'ing'
The last occurrence of a in 'C Programming' is 'amming'

C Programming Code Editor:

Previous C Programming: C strpbrk()
Next C Programming: C strspn()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/c-programming/string/c-strrchr.php