VOOZH about

URL: https://www.geeksforgeeks.org/c/strrchr-in-c/

⇱ strrchr() in C - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

strrchr() in C

Last Updated : 31 Mar, 2023

The strrchr() function in C locates the last occurrence of a character in a string and returns a pointer to it. It is a standard library function defined inside <string.h> header file.

Syntax :

char* strrchr( char* str, int chr );

Parameter:

  • str: specifies the pointer to the null-terminated string in which the search is to be performed.
  • chr: specifies the character to be searched.

Return Value:

  • The function returns a pointer to the last location of chr in the string if the chr is found.
  • If chr is not found, it returns a null pointer.

Example: 


Output
Last occurrence of k in GeeksforGeeks is at index 11

When the character is not present in the string

strrchr() function returns a NULL pointer if the searched character is not found in the given string.

Example:


Output
z is not present Geeks for Geeks 

Time Complexity: O(n),

Space Complexity: O(1),

where n is the length of the string.

Note: NULL character is also treated same as other character by strrchr() function, so we can also use it to find the end of the string.

Comment
Article Tags: