BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C strrchr() Function – C tutorial

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: c-programming

The strrchr() function searches the last occurrence of the specified character in the given string. This function works quite opposite to the function strchr() which searches the first occurrence of the character in the string.

C strrchr() function declaration

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

str – The string in which the character ch is searched.
ch – The character that needs to be searched

Return value of strrchr()

It returns the pointer to the last occurrence of the character in the string. Which means if we display the return value of the strrchr() then it should display the part of the string starting from the last occurrence of the specified character.

Example: strrchr() function

#include <stdio.h>
#include <string.h>
int main () {
   const char str[] = "This-is-just-a-test-string"; 
   const char ch = '-'; 
   char *p, *p2;

   p = strrchr(str, ch); 
   printf("String starting from last occurrence of %c is: %s\n", ch, p);

   p2 = strrchr(str, 'i'); 
   printf("String starting from last occurrence of 'i' is: %s\n", p2);

   return 0;
}

Output:

String starting from last occurrence of - is: -string
String starting from last occurrence of 'i' is: ing

Related Posts:

  1. C – strncpy() example
  2. C – strcpy() example
  3. C – strncmp() example
  4. C – strchr() example

Top Related Articles:

  1. C – loops in C programming with examples
  2. C strncat() Function with example
  3. C strspn() Function
  4. C strstr() Function – C tutorial
  5. C – switch case statement in C Programming with example

Tags: C-Library, C-String

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

C Programming Tutorial

  • C Tutorial
  • History of C
  • Features of C
  • Turbo C++ installation
  • First C Program
  • printf scanf
  • Variables in C
  • Data Types in C
  • C - Keywords
  • C Identifiers
  • C Comments
  • Operator precedence
  • C - if statement
  • C - if..else
  • C - for loop
  • C - while loop
  • C - do while loop
  • C - continue
  • C - break statement
  • C - switch..case
  • C - goto statement
  • C - Arrays
  • 2 D array
  • C - String
  • C - functions
  • Function call by reference
  • Function call by value
  • Array to function
  • C - Structures
  • C - Pointers
  • Pointer to Pointer
  • Pointers to functions
  • C - function pointers
  • Pointer & Array
  • C - File I/O
  • C Programming Examples

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap