Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <ctime>
- #include <forward_list>
- #include <string>
- using namespace std;
- void GenerateFile(const string& pattern, const string& file_name) {
- string text;
- int WORDS_COUNT = 5;
- srand(time(0));
- for (int i = 0; i < WORDS_COUNT; ++i) {
- int word_size = 1 + rand() % 10;
- string word;
- for (int j = 0; j < word_size; ++j) {
- word += (char)('a' + rand() % 25);
- }
- int pattern_chance = rand() % 11;
- if (pattern_chance == 0) {
- text += pattern;
- text += ' ';
- }
- if (i == 1 || i == 3) {
- text += pattern;
- text += ' ';
- }
- text += word;
- text += ' ';
- }
- ofstream fout(file_name, std::ios::binary);
- fout << text;
- fout.close();
- }
- void Print(const forward_list<char>& l) {
- auto it = l.begin();
- while (it != l.end()) {
- cout << *it++;
- }
- }
- int main() {
- setlocale(LC_ALL, "");
- string pattern, file_name;
- cin >> pattern >> file_name;
- GenerateFile(pattern, file_name);
- ifstream fin(file_name);
- if (!fin.is_open()) {
- cout << "Файл невозможно открыть " << file_name;
- return -1;
- }
- forward_list<char> text = { 't', 'a', 'c', ' ', ' ', 't', 'a', 'c' };
- char read_in;
- /*while (fin.get(read_in)) {
- text.push_front(read_in);
- }*/
- Print(text);
- string revers_patter;
- for (int i = pattern.size() - 1; i >= 0; i--) {
- revers_patter += pattern[i];
- }
- auto it = text.begin();
- string save;
- bool is_last_word = false;
- for (; it != text.end(); it++) {
- while (*it != ' ') {
- save += *it;
- auto is_end = it;
- is_end++;
- if (is_end == text.end()) {
- is_last_word = true;
- break;
- }
- it++;
- continue;
- }
- if (save.find(revers_patter) != string::npos) {
- if (is_last_word) {
- it = text.insert_after(it, ' ');
- }
- it = text.insert_after(it, save.begin(), save.end());
- if (is_last_word) {
- break;
- }
- it = text.insert_after(it, ' ');
- }
- save.erase();
- }
- cout << endl;
- Print(text);
- return 0;
- }
- //cat data.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement