0% found this document useful (0 votes)
25 views2 pages

Pattern Search

This document contains C code to implement the Knuth-Morris-Pratt (KMP) algorithm for string matching. It defines functions to: 1) Calculate the next state transition table for a pattern. 2) Build the transition function (TF) table representing a finite automaton for the pattern. 3) Search for all occurrences of the pattern in a text string using the TF table. It provides an example that searches for occurrences of the pattern "AABA" in a sample text.

Uploaded by

ragunath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

Pattern Search

This document contains C code to implement the Knuth-Morris-Pratt (KMP) algorithm for string matching. It defines functions to: 1) Calculate the next state transition table for a pattern. 2) Build the transition function (TF) table representing a finite automaton for the pattern. 3) Search for all occurrences of the pattern in a text string using the TF table. It provides an example that searches for occurrences of the pattern "AABA" in a sample text.

Uploaded by

ragunath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.#include<stdio.

h>
2.#include<string.h>
3.#define NO_OF_CHARS 256
4. 
5.int getNextState(char *pat, int M, int state, int x) {
6. // If the character c is same as next character in pattern,
7. // then simply increment state
8. if (state < M && x == pat[state])
9. return state + 1;
10.  
11. int ns, i; // ns stores the result which is next state
12.  
13. // ns finally contains the longest prefix which is also
suffix
14. // in "pat[0..state-1]c"
15.  
16. // Start from the largest possible value and stop when you
find
17. // a prefix which is also suffix
18. for (ns = state; ns > 0; ns--) {
19. if (pat[ns - 1] == x) {
20. for (i = 0; i < ns - 1; i++) {
21. if (pat[i] != pat[state - ns + 1 + i])
22. break;
23. }
24. if (i == ns - 1)
25. return ns;
26. }
27. }
28.  
29. return 0;
30. }
31.  
32. /* This function builds the TF table which represents Finite
Automata for a
33.  given pattern */
34. void computeTF(char *pat, int M, int TF[][NO_OF_CHARS]) {
35. int state, x;
36. for (state = 0; state <= M; ++state)
37. for (x = 0; x < NO_OF_CHARS; ++x)
38. TF[state][x] = getNextState(pat, M, state, x);
39. }
40.  
41. /* Prints all occurrences of pat in txt */
42. void search(char *pat, char *txt) {
43. int M = strlen(pat);
44. int N = strlen(txt);
45.  
46. int TF[M + 1][NO_OF_CHARS];
47.  
48. computeTF(pat, M, TF);
49.  
50. // Process txt over FA.
51. int i, state = 0;
52. for (i = 0; i < N; i++) {
53. state = TF[state][txt[i]];
54. if (state == M) {
55. printf("Pattern found at index %d\n", i - M + 1);
56. }
57. }
58. }
59.  
60. // Driver program to test above function
61. int main() {
62. char *txt = "AABAACAADAABAAABAA";
63. char *pat = "AABA";
64. search(pat, txt);
65. return 0;
66. }

Output:

$ gcc RepeatedStringSearch.c
$ ./a.out
 
Pattern found at index 0
Pattern found at index 9
Pattern found at index 13

You might also like