-
Notifications
You must be signed in to change notification settings - Fork 0
/
NaiveStringMatching.cpp
42 lines (33 loc) · 1.4 KB
/
NaiveStringMatching.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>
using namespace std;
/*———————————————————————————————————————————————————————————————————————————*/
void naivePatternSearch(string mainString, string pattern, int array[], int *index){
int patLen = pattern.size();
int strLen = mainString.size();
for(int i = 0; i <= (strLen - patLen); i++){
for(int j = 0; j < patLen; j++){ //check for each character of pattern if it is matched
if(mainString[i+j] != pattern[j])
break;
}
if(j == patLen) { //the pattern is found
(*index)++;
array[(*index)] = i;
}
}
}
/*———————————————————————————————————————————————————————————————————————————*/
int main(){
string mainString;
string pattern;
cout << "Enter the Main String : ";
cin >> mainString;
cout << "Enter the Pattern String : ";
cin >> pattern;
int locArray[mainString.size()];
int index = -1;
naivePatternSearch(mainString, pattern, locArray, &index);
cout << endl;
for(int i = 0; i <= index; i++) {
cout << "Pattern found at position: " << locArray[i] << endl;
}
}