A string manipulation function simulation of the outside chapter

Source: Internet
Author: User

First, STRCHR ()

function Prototypes: Char *strchr (const char *string, int c);

s TRCHR () function looks for the first occurrence of a character in a specified string, and if found, returns the location of the last occurrence of the specified character in the known string and returns null if it is not found. example: the string "Abdedef" is known, and the function returns "Dedef"

Char *my_strchr (const char *str,int c) {assert (str); while (*STR) {if (*str = = c) return (char *) str; Locate and return to this position str++;}                        return NULL; Not found}


Second, STRRCHR ()

function Prototypes: Char *strrchr (const char *string, int c);

The STRRCHR () function is exactly the opposite of the STRCHR () function, which is the last occurrence of the first number of occurrences of a character in a specified string, and, if found, returns the position where the last occurrence occurred, otherwise null is returned. also use the string above for example, the function returns the result is "Def"

Char *my_strrchr (const char *str,int c) {const char *p = Null;assert (str); while (*STR) {if (*str = = c) {p = str; Each occurrence of the position is recorded, p will always change}str++;} if (*str = = ') return (char *) P;else return NULL;}


Third, STRRSTR ()

We all know that there is strstr () in the library function, which is looking for the self-string, but like above, if we want to implement a function that returns the last occurrence of a substring in a specified string.

    So, since Strstr () is looking for the first occurrence and is in a positive order, look for the last time to reverse it.

char *my_strrstr (const char *str,const char * STRS,INT LEN1,INT LEN2) {const char *l_start = str+len1-1;   // Point to Str last const char *end = strs+len2-1;      // End points to STRs last assert (str), assert (STRs), while (LEN1) {str = l_start;while (len2 != 1)   &&  (*STRS == *STR)) {str--;strs--;len2--;                //pointer moves forward one bit, string length -1}l_start = str;                //the match is complete, remembering the first bit of the substring if (len2 ==  1) return  (char *) l_start;if (*str != *strs) {l_start = l_start-1;    strs = end;} len1--;} Return null;} 


A string manipulation function simulation of the outside chapter

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.