Naive Pattern Searching


void search(char *pat, char *txt)
    int M = strlen(pat);
    int N = strlen(txt);
    int i;
    /* A loop to slide pat[] one by one */
    for (i = 0; i <= N - M; i++)
        int j;
        /* For current index i, check for pattern match */
        for (j = 0; j < M; j++)
            if (txt[i+j] != pat[j])
        if (j == M)  // if pat[0...M-1] = txt[i, i+1, ...i+M-1]
           printf("Pattern found at index %d \n", i);

/* Driver program to test above function */
int main()
   char txt[] = "AABAACAADAABAAABAA";
   char pat[] = "AABA";
   search(pat, txt);
   return 0;

Pattern found at index 0
Pattern found at index 9
Pattern found at index 13


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s