Jump to content

1 post in this topic

Recommended Posts

Posted (edited)

I'm trying to make A code for my AI to search a database of predetermined code snippets that it can use to write its own code. Maybe @ZahirSher or @shmoo or @DiDA could help please. Like where did I mess up? The error said- invalid code and didn't give me a line. Here is the code:

 

const char *kmp_search(const char *text, const char *pattern)
{
    int *T;
    int i, j;
    const char *result = NULL;
 
    if (pattern[0] == '\0')
        return text;
 
    /* Construct the lookup table */
    T = (int*) malloc((strlen(pattern)+1) * sizeof(int) );
    T[0] = -1;
    for (i=0; pattern != '\0'; i++) {
        T[i+1] = T + 1;
        while (T[i+1] > 0 && pattern != pattern[T[i+1]-1])
            T[i+1] = T[T[i+1]-1] + 1;
    }
 
    /* Perform the search */
    for (i=j=0; text != '\0'; ) {
        if (j < 0 || text == pattern[j]) {
            ++i, ++j;
            if (pattern[j] == '\0') {
                result = text+i-j;
                break;
            }
        }
        else j = T[j];
    }
 
    free(T);
    return result;
}

 

Updated by Donald J Trump

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...

Important Information

We would like to place cookies on your device to help make this website better. The website cannot give you the best user experience without cookies. You can accept or decline our cookies. You may also adjust your cookie settings. Privacy Policy - Guidelines