-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17.18.cpp
More file actions
57 lines (50 loc) · 1.21 KB
/
17.18.cpp
File metadata and controls
57 lines (50 loc) · 1.21 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
#include <regex>
#include <iostream>
#include <string>
#include <fstream>
#include <set>
using std::regex;
using std::regex_search;
using std::sregex_iterator;
using std::smatch;
using std::cout;
using std::endl;
using std::cin;
using std::string;
using std::ifstream;
using std::getline;
using std::set;
string readFile(std::istream &is) {
string content;
bool first = true;
for (string line; getline(is, line); ) {
if (!first) {
content += ' ';
}
content += line;
first = false;
}
return content;
}
int main() {
// Regex Pattern
string pattern = "[^c]ei";
pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";
regex r(pattern, regex::icase);
// Legal words
set<string> legalWords = {"albeit", "neighbor", "being", "reign", "heir"};
// Read file into string
ifstream infile("text.txt");
string file = readFile(infile);
// Report errors
for (sregex_iterator it(file.cbegin(), file.cend(), r), end;
it != end; ++it) {
if (legalWords.find(it->str()) != legalWords.cend()) continue;
auto pos = it->prefix().length();
pos = pos > 40 ? pos - 40 : 0;
cout << it->prefix().str().substr(pos)
<< "\n\t\t>>> " << it->str() << " <<<\n"
<< it->suffix().str().substr(0, 40) << endl;
}
return 0;
}