Advertisement
35657

Untitled

May 25th, 2024
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <fstream>
  6. #include <Windows.h>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. int main() {
  12.     SetConsoleCP(1251);
  13.     SetConsoleOutputCP(1251);
  14.  
  15.     ifstream fin;
  16.     ofstream fout;
  17.  
  18.     fin.open("file.txt");
  19.     fout.open("file2.txt");
  20.  
  21.     if (!fin.is_open() || !fout.is_open()) {
  22.         cout << "Ошибка открытия файла" << endl;
  23.     }
  24.     else {
  25.        
  26.         int count = 0;
  27.         string word, find_word, new_word, buf;
  28.  
  29.         cout << "Введите слово для поиска: ";
  30.         getline(cin, find_word);
  31.         cout << "Введите слово для замены: ";
  32.         getline(cin, new_word);
  33.  
  34.         while (!fin.eof()) {
  35.             getline(fin, word);
  36.             for (int i = 0; i < word.size(); i++) {
  37.                 if (word[i] == find_word[0]) {
  38.                     for (int j = 0; j < min(word.size(), find_word.size()); j++) {
  39.                         if (word[i] != find_word[j]) {
  40.                             break;
  41.                         }
  42.                         buf.push_back(word[i]);
  43.                         i++;
  44.                     }
  45.                     if (buf == find_word) {
  46.                         fout << new_word;
  47.                         count++;
  48.                     }
  49.                     else {
  50.                         fout << buf;
  51.                     }
  52.                     buf.clear();
  53.                 }
  54.                 fout << word[i];
  55.             }
  56.             fout << endl;
  57.         }
  58.  
  59.         cout << "Произведено замен: " << count << endl;
  60.  
  61.         fin.close();
  62.         fout.close();
  63.         remove("file.txt");
  64.         rename("file2.txt", "file.txt");
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement