-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuestion 5(ii).cpp
More file actions
64 lines (60 loc) · 1.83 KB
/
Question 5(ii).cpp
File metadata and controls
64 lines (60 loc) · 1.83 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<iostream>
#include<string>
#include<vector>
using namespace std;
bool check( vector <string> text ,vector<string> pattern){
long text_size = text.size(); // size of text vector
long pattern_size = pattern.size(); // size of pattern vector
if( text_size < pattern_size ) {
// if size of pattern is greater than size of text , then it's not possible to search pattern in text. so return false
return false;
}
int itr1 = 0; // for text pointer
int itr2 = 0; // for pattern pointer
while(itr1 < text_size && itr2 < pattern_size ) {
if( text[itr1].compare(pattern[itr2]) == 0 ) { // if matched , then increment both pointer
itr1++;
itr2++;
}
else {
itr1++; // otherwise increment text pointer only
}
}
if ( itr2 == pattern_size ) {
// if pattern pointer is equal to size of pattern , then pattern exists in the text
return true;
}
else {
// otherwise text does not contain pattern
return false;
}
}
int main() {
vector <string> text;
vector <string> pattern;
cout << "\nEnter Text\n";
string temp ="";
while(true) {
getline(cin , temp);
if (temp == "-1" ) {
break;
}
text.push_back(temp);
}
temp = " ";
cout << "\nEnter Pattern\n";
while(true) {
getline(cin ,temp);
if (temp == "-1" ) {
break;
}
pattern.push_back(temp);
}
if( check( text , pattern ) ) {
cout<<"\nText contains Pattern\n";
}
else {
cout<<"\nText does not contain Pattern\n";
}
return 0;
}