Strrchr () function

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.