C Program for KMP Algorithm for Pattern Searching

In this problem, we are given two strings a text and a pattern. Our task is to create a program for KMP algorithm for pattern search, it will find all the occurrences of pattern in text string.

The KMP (Knuth Morris Pratt) algorithm is an efficient string matching algorithm that preprocesses the pattern to avoid unnecessary comparisons. It uses a failure function to skip characters when a mismatch occurs.

Syntax

void KMPSearch(char* text, char* pattern);
void computeLPSArray(char* pattern, int M, int* lps);

How KMP Algorithm Works

The KMP algorithm works in two phases −

  • Preprocessing: Build the LPS (Longest Proper Prefix which is also Suffix) array
  • Searching: Use the LPS array to skip characters during pattern matching
KMP Algorithm Process Text: x y z t r w q x y z Pattern: x y z LPS Array: 0 0 0 Matches found at: Index 0: xyz Index 7: xyz

Example: KMP Pattern Searching

This example demonstrates the complete KMP algorithm implementation −

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* Function to compute LPS array for pattern */
void computeLPSArray(char* pattern, int M, int* lps) {
    int length = 0;
    lps[0] = 0;
    int i = 1;
    
    while (i < M) {
        if (pattern[i] == pattern[length]) {
            length++;
            lps[i] = length;
            i++;
        } else {
            if (length != 0) {
                length = lps[length - 1];
            } else {
                lps[i] = 0;
                i++;
            }
        }
    }
}

/* KMP pattern searching function */
void KMPSearch(char* text, char* pattern) {
    int M = strlen(pattern);
    int N = strlen(text);
    
    /* Create LPS array */
    int* lps = (int*)malloc(M * sizeof(int));
    computeLPSArray(pattern, M, lps);
    
    int i = 0; /* index for text */
    int j = 0; /* index for pattern */
    
    while (i < N) {
        if (pattern[j] == text[i]) {
            j++;
            i++;
        }
        
        if (j == M) {
            printf("Found pattern at index %d<br>", i - j);
            j = lps[j - 1];
        } else if (i < N && pattern[j] != text[i]) {
            if (j != 0) {
                j = lps[j - 1];
            } else {
                i = i + 1;
            }
        }
    }
    
    free(lps);
}

int main() {
    char text[] = "xyztrwqxyzfg";
    char pattern[] = "xyz";
    
    printf("Text: %s<br>", text);
    printf("Pattern: %s<br>", pattern);
    printf("Pattern occurrences:<br>");
    
    KMPSearch(text, pattern);
    
    return 0;
}
Text: xyztrwqxyzfg
Pattern: xyz
Pattern occurrences:
Found pattern at index 0
Found pattern at index 7

Time and Space Complexity

Aspect Complexity Description
Time Complexity O(N + M) N = text length, M = pattern length
Space Complexity O(M) For LPS array storage
Preprocessing O(M) Building LPS array

Key Points

  • The LPS array stores the length of the longest proper prefix which is also a suffix
  • KMP algorithm never moves backwards in the text string
  • It's more efficient than naive pattern searching for large texts
  • Memory allocation for LPS array should be freed to prevent memory leaks

Conclusion

The KMP algorithm provides an efficient solution for pattern searching with linear time complexity. It uses preprocessing to build an LPS array that helps skip unnecessary character comparisons during the search process.

Updated on: 2026-03-15T12:50:15+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements