C++ Program to Implement Knuth-Morris-Pratt Algorithm for Pattern Searching

This C++ program implements the Kunth-Morris-Pratt Algorithm for string matching. A text and a pattern is given as input. The pattern is searched for in the text and all instances of the pattern are given as output.

This C++ program is successfully compiled and run on Code::Blocks 10.05, a C++ compiler. The program output is given below.

  1.  
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5.  
  6. void computeLPSArray(char *pat, int M, int *lps);
  7.  
  8. void KMPSearch(char *pat, char *txt)
  9. {
  10.     int M = strlen(pat);
  11.     int N = strlen(txt);
  12.  
  13.     // create lps[] that will hold the longest prefix suffix values for pattern
  14.     int *lps = (int *)malloc(sizeof(int)*M);
  15.     int j  = 0;  // index for pat[]
  16.  
  17.     // Preprocess the pattern (calculate lps[] array)
  18.     computeLPSArray(pat, M, lps);
  19.  
  20.     int i = 0;  // index for txt[]
  21.     while(i < N)
  22.     {
  23.       if(pat[j] == txt[i])
  24.       {
  25.         j++;
  26.         i++;
  27.       }
  28.  
  29.       if (j == M)
  30.       {
  31.         printf("Found pattern at index %d \n", i-j);
  32.         j = lps[j-1];
  33.       }
  34.  
  35.       // mismatch after j matches
  36.       else if(pat[j] != txt[i])
  37.       {
  38.         // Do not match lps[0..lps[j-1]] characters,
  39.         // they will match anyway
  40.         if(j != 0)
  41.          j = lps[j-1];
  42.         else
  43.          i = i+1;
  44.       }
  45.     }
  46.     free(lps); // to avoid memory leak
  47. }
  48.  
  49. void computeLPSArray(char *pat, int M, int *lps)
  50. {
  51.     int len = 0;  // lenght of the previous longest prefix suffix
  52.     int i;
  53.  
  54.     lps[0] = 0; // lps[0] is always 0
  55.     i = 1;
  56.  
  57.     // the loop calculates lps[i] for i = 1 to M-1
  58.     while(i < M)
  59.     {
  60.        if(pat[i] == pat[len])
  61.        {
  62.          len++;
  63.          lps[i] = len;
  64.          i++;
  65.        }
  66.        else // (pat[i] != pat[len])
  67.        {
  68.          if( len != 0 )
  69.          {
  70.            // This is tricky. Consider the example AAACAAAA and i = 7.
  71.            len = lps[len-1];
  72.  
  73.            // Also, note that we do not increment i here
  74.          }
  75.          else // if (len == 0)
  76.          {
  77.            lps[i] = 0;
  78.            i++;
  79.          }
  80.        }
  81.     }
  82. }
  83.  
  84. // Driver program to test above function
  85. int main()
  86. {
  87.    char *txt = "ABABDABACDABABCABAB";
  88.    char *pat = "ABABCABAB";
  89.    KMPSearch(pat, txt);
  90.    return 0;
  91. }

 
Output
 
Found pattern at index 10

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.