Advertisement
Wackierleaf

Sem_2_Lab_8

Mar 26th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <windows.h>
  5.  
  6. using namespace std;
  7. void replace(string &s, const string &search, const string &replace) {
  8.     for (size_t pos = 0; ; pos += replace.length())
  9.     {
  10.         pos = s.find(search, pos);
  11.         if (pos == string::npos) break;
  12.         s.erase(pos, search.length());
  13.         s.insert(pos, replace);
  14.     }
  15. }
  16.  
  17. int main()
  18. {
  19.     SetConsoleCP(1251);
  20.     SetConsoleOutputCP(1251);
  21.     cout << "Введите последовательность которую нужно поместить в файл: ";
  22.     string sec;
  23.     getline(cin, sec);
  24.     cout << "Введите последовательность P1: ";
  25.     string str;
  26.     getline(cin, str);
  27.     cout <<endl << "Введите последовательность P2: ";
  28.     string sub_str;
  29.     getline(cin, sub_str);
  30.     ofstream fout;
  31.     fout.open("data.txt");
  32.     fout << sec;
  33.     fout.close();
  34.     string line;
  35.     ifstream finp;
  36.     finp.open("data.txt");
  37.     getline(finp, line);
  38.     finp.close();
  39.     replace(line, str, sub_str);
  40.     fout.open("data.txt", ios::app);
  41.     fout <<endl << line;
  42.     fout.close();
  43.     system("pause");
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement