-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathKMP String Match.cpp
More file actions
55 lines (44 loc) · 837 Bytes
/
KMP String Match.cpp
File metadata and controls
55 lines (44 loc) · 837 Bytes
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
#include<stdlib.h>
#include<iostream>
using namespace std;
// code to find string within a string using KMP algorithm (in-development)
int main(){
//KMP Algorithm, String matching
int n=0,m=0,i=0,j=0,arr[100],g=0,k=0,c=0;
cout<<"Input size of S(String), Size of T(Pattern)"<<endl;
cin>>n;
cin>>m;
char S[n],T[m];
while(i<n){
cin>>S[i];
i++;
}
cout<<"Done, Input T"<<endl;
i=0;
while(i<m){
cin>>T[i];
i++;
}
i=0;
for(i=0;i<n;i++){
if(S[i]==T[0]){
for(j=0;j<m;j++){
if(S[i+j]==T[j]){
c++;
}
}
if(c==m){
g++;
arr[k]=i;
k++;
}
}
}
i=0;
cout<<g<<" matches found at indices ";
while(arr[i]!='\n'){
cout<<arr[i]<<" ";
i++;
}
return 0;
}