-
Notifications
You must be signed in to change notification settings - Fork 523
/
kmp_string_matching.cpp
42 lines (33 loc) · 1.09 KB
/
kmp_string_matching.cpp
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
#include <iostream>
#include <string>
using namespace std;
// Function to perform the naive string matching algorithm
void stringMatching(string text, string pattern) {
int textLength = text.length();
int patternLength = pattern.length();
// Loop through the text to check for the pattern at each position
for (int i = 0; i <= textLength - patternLength; i++) {
int j;
// For the current position in text, check if the pattern matches
for (j = 0; j < patternLength; j++) {
if (text[i + j] != pattern[j]) {
break; // Mismatch, break out of inner loop
}
}
// If we found a match, the inner loop will have completed
if (j == patternLength) {
cout << "Pattern found at index " << i << endl;
}
}
}
int main() {
string text, pattern;
// Take input from the user
cout << "Enter the text: ";
getline(cin, text);
cout << "Enter the pattern to search: ";
getline(cin, pattern);
// Perform string matching
stringMatching(text, pattern);
return 0;
}