-
Notifications
You must be signed in to change notification settings - Fork 127
/
string-match.cpp
54 lines (42 loc) · 1.23 KB
/
string-match.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
43
44
45
46
47
48
49
50
51
52
53
54
/******************************************************************************
String Matching Algorithm helps to find if two string matches or not || if a pattern
can be found in a text or not.
Brute-Force approach to match string is dicussed below.
*******************************************************************************/
#include <iostream>
using namespace std;
int stringMatch(string text, string pattern){
int text_size = text.size();
int pattern_size = pattern.size();
for(int i = 0; i<=(text_size - pattern_size); i++){
int j = 0;
while(j<pattern_size && text[i+j] == pattern[j]){
j++;
}
if(j == pattern_size){
return i;
}
}
return -1;
}
int main()
{
string text = "Hello World";
string pattern1 = "ello";
string pattern2 = "asff";
int ans1 = stringMatch(text, pattern1);
int ans2 = stringMatch(text, pattern2);
if(ans1 != -1){
cout<<"Pattern1 is found at "<<ans1<<endl;
}
else{
cout<<"Pattern1 is not found."<<endl;
}
if(ans2 != -1){
cout<<"Pattern2 is found at "<<ans2<<endl;
}
else{
cout<<"Pattern2 is not found."<<endl;
}
return 0;
}