Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
int horspoolstringmatching(const string &P, const string &T) {

    int table[127];

    int k, flag = 0;

    int m = T.length();
    int n = P.length();
    int i, j;


    for (i = 0; i < 127; i++)
        table[i] = m;
    for (j = 0; j < m - 1; j++)
        table[P[j]] = m - 1 - j;

     i = m - 1;





    while (i <= n - 1) {

        k = 0;
        while (k <= m - 1 && T[m - 1 - k] == P[i - k]) 
            k++;


        if (k == m) {
            return i - m + 1;
            flag = 1;
            break;
        }


        else {
            i = i + table[P[i]];
        }

    }
    if (!flag)
        return -1;


}


What I have tried:

for example if I try to find "abc" int the text "aAaaabc" it works but if I change "A" with "a" it doesn't works or if I delete "A" it doesn't works what is wrong with this code.
Posted
Updated 25-Nov-17 7:57am
v2
Comments
Richard MacCutchan 26-Nov-17 8:58am    
Largely because it is impossible to understand. Why not use the string functions that are provide?
KarstenK 26-Nov-17 13:05pm    
Google after strtok C function :-O

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900