Pattern matching algorithm implemented in C ++

Source: Internet
Author: User
# Include <iostream. h>
# Include <string. h>
Int index (char * s, char * t, int POS)
// Return the position of the substring T following the POs character in the primary string S.
// If not, the return value of the function is 0.
// Where T is not empty, 1 <= POS <= strlen (s)
{
Int I = Pos;
Int J = 1;
While (I <= strlen (s) & J <= strlen (t ))
{
If (s [I-1] = T [J-1]) // continue to compare subsequent characters
{
++ I;
++ J;
}
Else // returns the pointer and starts matching again.
{
I = I-j + 2;
J = 1;
}
}
If (j> strlen (t ))
Return I-strlen (t );
Else
Return 0;
}
Void main () // main function, used to test the pattern matching algorithm
{
Char * A = "ahdindfce ";
Char * B = "IND ";
Cout <index (a, B, 0)> <Endl;
}

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.