Usually see STRRCHR This function, people naturally want to ask the library is there strrstr this function???
The answer is: No.
But we can implement a STRRSTR function, its function is to find the last occurrence of the substring, if the call will return this address, if not found, return an empty address.
Implementation scenario: There are two implementations of this function.
The first is to look forward from behind and return to this address the first time you find it.
The second is to look backwards, use a tag to record the first position found, and then update the label the second time it is found, the last tag is the address we are looking for.
Looking forward from behind:
#include <stdio.h> #include <stdlib.h> #include <assert.h> #include <string.h>char *my_ Strrstr (CONST&NBSP;CHAR&NBSP;*DST,&NBSP;CONST&NBSP;CHAR&NBSP;*SRC) { assert (dst ); //asserts the string to determine whether it is an empty assert (src ); const char *pdst = dst; const char *psrc = src; char *tmp = null ; while (*PDST) //because to look forward from the back, first let pdst point to ' ' { pdst++; } while (pdst >= dst ) // When PDST is greater than DST, it indicates that the DST string is not finished { if ( tmp= (char *) strstr (PDST,&NBSP;PSRC&NBSP;=&NBSP;SRC) //use Strstr help Find, find save to tmp return tmp; pdst--; } return null ;} Int main () { char arr[30]; char arr1[20]; &NBSP;&NBSP;&NBSP;SCANF ( "%s%s", arr, arr1); char *ret=my_strrstr (ARR,&NBSP;ARR1) printf ( "% #p \ n", ret) system ( " Pause "); return 0;}
High efficiency when the string is large
< Span style= "font-size:32px" > from front to back:
Char *my_strrstr (CONST&NBSP;CHAR&NBSP;*DST,&NBSP;CONST&NBSP;CHAR&NBSP;*SRC) { assert (dst ); assert (src ); const char *pdst = dst; const char *psrc = src; char *right= NULL ; while (*dst ) { while (*PDST&NBSP;==&NBSP;*PSRC) { if (*pdst== ' ) //if pdst points to '/', the target array is already found return right= (char *) dst; else { pdst++; psrc++; } } if (*psrc == ' ) // SRC already appears in DST, save this address right = (Char *) dst ; pdst = ++ dst; psrc = src; } return right;} Int main () { char arr1[50] = { 0 }; char arr2[40] = { 0 }; char * PLACE&NBSP;=&NBSP;NULL&NBSP;;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ( "%s%s", ARR1,&NBSP;ARR2); place = my_strrstr (ARR1,&NBSP;ARR2); priNTF ( "% #p \ n", place); system ( "pause"); return 0;}
Extremely low efficiency when the target string is large
Strtok
Function: A string can be read separated by delimiter
Prototype: char * strtok (char *str,const char *sep);
STR is the string to be read, which contains a number of characters in Sep. Sep points to the delimiter string. Strtok find the next mark in STR and replace it with '/'.
Usage:
Example: arr[]= "[email protected] #ef $gh #"; Sep[]= "@#$".
separate STR According to the delimiters that appear in Sep. If the parameter arr is not empty, then Strtok will find the first tag, assign it as ' \ x ', and save its next position.
call Strtok (ARR,SEP) for the first time; return to the first address, Save the address of C.
call Strtok (NULL,SEP) for the second time; if the str parameter is NULL, Strtok looks for the next delimiter from the position of C. that is, find the # and change the # to ' + ' and return to the address of C.
then call Strtok and the second time similar: If the str parameter is NULL, Strtok looks for the next delimiter in the same string from where it was last saved.
Strtok implementation:
Char *my_strtok (CHAR&NBSP;*DST,&NBSP;CHAR&NBSP;CONST&NBSP;*&NBSP;SEP) { ASSERT (dst ); // Asserts whether the string is empty assert (sep ); static char * pdst=null; //declares a static local variable char *origin = null ; if (dst ) pdst = dst; origin = pdst; //Record start position while (&NBSP;!STRCHR (SEP&NBSP;,&NBSP;*PDST) | | *PDST) //look for separators when encountering '/' program ends { pdst++; } if (*PDST) //If a delimiter is found &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;*PDST ++ = '; Replace the delimiter with ' return origin; ' }int main () { char *ret = null ; char arr[20]; char *arr1 = "@#$" ; scanf ( "%s", arr); for (Ret = strtok (ARR,&NBSP;ARR1); ret != null; ret = strtok ( NULL, &NBSP;ARR1)) &NBSP;&Nbsp; printf ( "%s\n", ret); system ( "pause"); return 0;}
String function External text