C-implemented string lookup Function

Source: Internet
Author: User

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 ;}

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.