-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQ5(i).cpp
More file actions
94 lines (88 loc) · 2.93 KB
/
Q5(i).cpp
File metadata and controls
94 lines (88 loc) · 2.93 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<iostream>
#include<string>
#include<vector>
#include <fstream>
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() {
// text_file.txt and pattern_file.txt and this program should be in the same file.
// reading from text_file.txt and storing it in text vector
vector <string> text;
ifstream myfile1 ("text_file.txt");
if ( myfile1 ) {
string line;
while ( getline (myfile1 , line) ) {
string temp = "";
for(int i = 0; i < line.length(); ++i) {
if( line.substr(i,3).compare(" , ") != 0) { // getting ' , ' then store it
temp += line[i];
}
else {
text.push_back(temp);
temp = "";
i = i + 2;
}
}
}
myfile1.close();
}
else {
cout << "\nUnable to open text file\n";
}
// reading from pattern_file.txt and storing it in pattern vector
vector <string> pattern;
ifstream myfile2 ("pattern_file.txt");
if (myfile2) {
string line;
while ( getline (myfile2 , line) ) {
string temp = "";
for(int i = 0; i < line.length(); ++i) {
if( line.substr(i,1).compare(" , ") != 0) { // getting ' , ' then store it
temp += line[i];
}
else {
pattern.push_back(temp);
temp = "";
i = i + 2;
}
}
}
myfile2.close();
}
else {
cout << "\nUnable to open pattern file\n";
}
if( check( text , pattern ) ) {
cout<<"\nText contains Pattern\n";
}
else {
cout<<"\nText does not contain Pattern\n";
}
return 0;
}