Strrchr () function:
The role of the strrchr () function is:
Searches for the last occurrence position of a string in another string, and returns all characters starting from this position in the string until the end of the string;
If the specified character cannot be found, the function returns false.
Char * strrchr (
Char * STR,
Int C
);
Analysis of strrchr () Functions
Function prototype: extern char * strrchr (const char * s, int C)
Parameter description: S is a string pointer, and C is a character to be searched.
Database Name: # include <string. h>
Function: locate the last occurrence of character C in the S string.
Return Description: If the STR contains the CH character, the pointer at the CH position is returned; otherwise, null is returned.
Note:
Str
Required parameter. Specify the string to be searched (string pointer)
C
Required parameter. Specifies the character object to be searched. If it is a number, it will search for characters that match the ASCII value of the number.
Strrchr () function source code:
/* Search for the position where character C is last displayed in the S string */
Char * strrchr (const char * s, int C)
{
Register const char * found, * P;
C = (unsigned char) C;
// If the searched character is an Terminator, use the strchr function to return the position of the Terminator.
If (C = '/0 ')
Return strchr (S, '/0 ');
// It is very important to assign the initial value to the return value (the character address to be searched.
Found = NULL;
// Search for character C from the current string and assign the return pointer to P. If P is not equal to null, the loop is executed.
// If P is null, the entire string is searched and the loop is exited.
While (P = strchr (S, C ))! = NULL)
{
// Temporarily store the character addresses found.
Found = P;
// Truncate the searched string (start with the next character address of return address P ).
S = p + 1;
}
// If no character is found, the loop is not executed, and found returns the initial value null.
// If a character is found, the pointer address assigned to found by P is returned.
Return (char *) found;
}
Function example:
# Include <string. h>
# Include <stdio. h>
Void main ()
{
Char * PCH = "www.inkcool.com ";
Char * pfind = strrchr (PCH ,'.');
If (pfind! = NULL)
{
Printf ("% s/n", pfind); // you can directly printf (pfind); printf ("/N"); the expression on the left is an expression that combines two into one;
}
}
The returned result is. com // note that '.' is returned instead of COM.