-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_Native_String_Matching.cpp
More file actions
41 lines (34 loc) · 1.01 KB
/
5_Native_String_Matching.cpp
File metadata and controls
41 lines (34 loc) · 1.01 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
#include <../stringMatching.h>
#include <bits/stdc++.h>
using namespace std;
class NaiveAlgorithm : public StringMatching {
bool matchString(int indexToSearch) {
for (int i = 0; i < lengthOfSearchString; i++) {
if (dataString[indexToSearch + i] != searchString[i]) return false;
}
return true;
}
public:
NaiveAlgorithm(string dataString, string searchString)
: StringMatching(dataString, searchString) {}
void findMatches() {
for (int i = 0; i < lengthOfDataString - lengthOfSearchString + 1; i++) {
if (matchString(i)) {
printMessage(i);
totalMatches++;
}
}
cout << "Total matches are : " << totalMatches << endl;
}
};
int main() {
string dataInput, searchInput;
cout << "Enter Data String : ";
getline(cin, dataInput);
cout << "Enter String to Search in Data : ";
getline(cin, searchInput);
cout << endl << "By Naive Algorithm : " << endl;
NaiveAlgorithm naiveAlgorithm(dataInput, searchInput);
naiveAlgorithm.findMatches();
return 0;
}