strrchr() in C Last Updated : 31 Mar, 2023 Comments Improve Suggest changes 5 Likes Like Report 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: C // C program to illustrate // the strrchr() function #include <stdio.h> #include <string.h> int main() { // initializing string char str[] = "GeeksforGeeks"; // character to be searched char chr = 'k'; // Storing pointer returned by char* ptr = strrchr(str, chr); // getting the position of the character if (ptr) { printf("Last occurrence of %c in %s is at index %d", chr, str, ptr - str); } // condition for character not present else { printf("%c is not present in %s ", chr, str); } return 0; } OutputLast occurrence of k in GeeksforGeeks is at index 11When 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: C // C program to illustrate // the strrchr() function #include <stdio.h> #include <string.h> int main() { // creating some string char str[] = "GeeksforGeeks"; char* ptr; // The character to be searched char chr = 'z'; // pointer returned by strrchr() ptr = strrchr(str, chr); // ptr-string gives the index location if (ptr) { printf("Last occurrence of %c in %s is at %d", chr, str, ptr - str); } // If the character we're searching is not present in // the array else { printf("%c is not present in %s ", chr, str); } return 0; } Outputz 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 A AmanSrivastava1 Follow 5 Improve A AmanSrivastava1 Follow 5 Improve Article Tags : Misc C Language C-Library Explore C BasicsC Language Introduction6 min readIdentifiers in C3 min readKeywords in C2 min readVariables in C4 min readData Types in C3 min readOperators in C8 min readDecision Making in C (if , if..else, Nested if, if-else-if )7 min readLoops in C6 min readFunctions in C5 min readArrays & StringsArrays in C4 min readStrings in C5 min readPointers and StructuresPointers in C7 min readFunction Pointer in C6 min readUnions in C3 min readEnumeration (or enum) in C5 min readStructure Member Alignment, Padding and Data Packing8 min readMemory ManagementMemory Layout of C Programs5 min readDynamic Memory Allocation in C7 min readWhat is Memory Leak? How can we avoid?2 min readFile & Error HandlingFile Handling in C11 min readRead/Write Structure From/to a File in C3 min readError Handling in C8 min readUsing goto for Exception Handling in C4 min readError Handling During File Operations in C5 min readAdvanced ConceptsVariadic Functions in C5 min readSignals in C language5 min readSocket Programming in C8 min read_Generics Keyword in C3 min readMultithreading in C9 min read Like