1. c ++ seems to have implemented this method. I used the C code to implement it again without implementing algorithm optimization. It can solve basic string search needs.
/*** Return the Index * zgr 2013-05-23 * Buf-source string * offset-source string offset-search from offset * expr-matching string * bufmaxlen-source String Length */INT findfirst (const char * Buf, int offset, const char * expr, int bufmaxlen) {If (BUF = NULL | offset <0 | Offset> bufMaxLen-1) {return-1;} const char * P1; const char * P2; P1 = BUF + offset; P2 = expr; int curoffset = offset; int sametimes = 0; int exproffset =-1; while (curoffset <bufmaxlen) {If (* P1 = * P2 & sametimes = 0) {exproffset = curoffset; P1 ++; P2 ++; curoffset ++; sametimes ++ ;} else if (* P1 = * P2 & sametimes <strlen (expr) {p1 ++; P2 ++; curoffset ++; sametimes ++; if (sametimes = strlen (expr) {return exproffset ;}} else {If (exproffset >=0) {p1 = BUF + exproffset + 1; P2 = expr; curoffset = exproffset + 1; sametimes = 0; exproffset =-1;} else {p1 ++; curoffset ++ ;}} return-1 ;}
This is to search from the back
/*** Return the Index * zgr 2013-05-23 * Buf-source string * expr-match string * bufmaxlen-source String Length of the first character of the last item that the Buf finds exactly matches expr */INT findlast (const char * Buf, const char * expr, int bufmaxlen) {If (BUF = NULL | bufmaxlen <0 | expr = NULL) {return-1;} const char * P1; const char * P2; P1 = BUF + bufMaxLen-1; P2 = expr + strlen (expr)-1; int curoffset = bufMaxLen-1; int sametimes = 0; int exproffset =-1; while (curoffset> = 0) {If (* P1 = * P2 & sametimes = 0) {exproffset = curoffset; P1 --; P2 --; curoffset --; sametimes ++;} else if (* P1 = * P2 & sametimes <strlen (expr) {sametimes ++; If (sametimes = strlen (expr )) {return exproffset-(strlen (expr)-1);} P1 --; P2 --; curoffset --;} else {If (exproffset> = 0) {p1 = BUF + (exprOffSet-1); P2 = expr + strlen (expr)-1; curoffset = exprOffSet-1; sametimes = 0; exproffset =-1;} else {p1 --; curoffset -- ;}} return-1 ;}