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