-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMPstringMatching.c
More file actions
72 lines (57 loc) · 1.45 KB
/
KMPstringMatching.c
File metadata and controls
72 lines (57 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <string.h>
void computeLPS(char *pattern, int M, int lps[]) {
int length = 0;
lps[0] = 0; // lps[0] is always 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++;
}
}
}
printf("\nLPS Array : ");
for(int i=0;i<strlen(pattern);i++)
printf(" %d ,",lps[i]);
}
void KMPSearch(char *text, char *pattern) {
int M = strlen(pattern);
int N = strlen(text);
int lps[M]; // Longest prefix suffix array for the pattern
computeLPS(pattern, M, lps);
int i = 0; // Index for text[]
int j = 0; // Index for pattern[]
while (i < N) {
if (pattern[j] == text[i]) {
i++;
j++;
}
if (j == M) {
printf("\nPattern found at index %d\n", i - j);
j = lps[j - 1];
} else if (i < N && pattern[j] != text[i]) {
if (j != 0)
j = lps[j - 1];
else
i++;
}
}
}
int main() {
char text[1000];
char pattern[100];
printf("Enter the text: ");
gets(text);
printf("Enter the pattern: ");
gets(pattern);
printf("Pattern matching results:\n");
KMPSearch(text, pattern);
}