Advertisement
35657

Untitled

Jul 5th, 2023
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <vector>
  6.  
  7. #include <windows.h>  // для работы SetConsoleCP, SetConsoleOutputCP
  8.  
  9.  
  10. using namespace std;
  11.  
  12.  
  13. int main() {
  14.  
  15.     SetConsoleCP(1251); // установка кодовой страницы windows cp 1251 в поток ввода
  16.     SetConsoleOutputCP(1251); // установка кодовой страницы windows cp 1251 в поток вывода
  17.  
  18.     ifstream fin;
  19.     fin.open("C:/Users/PC/Desktop/file.txt");
  20.  
  21.     if (!fin.is_open()) {
  22.         cout << "Ошибка открытия файла" << endl;
  23.     }
  24.     else {
  25.         int count = 0;
  26.         string word;
  27.         string user_word;
  28.         cout << "Введите строку для поиска: ";
  29.         getline(cin, user_word);
  30.         const int size = user_word.size();
  31.         int k = 0;
  32.         while (!fin.eof()) {
  33.             getline(fin, word);
  34.             for (int i = 0; i < word.size(); i++) {
  35.                 if (word[i] == user_word[0]) {
  36.                     for (int j = 0; j < min(size, word.size()); j++) {
  37.                         if (word[i] != user_word[j]) {
  38.                             k = 0;
  39.                             break;
  40.                         }
  41.                         k++;
  42.                         i++;
  43.                     }
  44.                     if (k == size) {
  45.                         count++;
  46.                     }
  47.                     k = 0;
  48.                 }
  49.             }
  50.         }
  51.         fin.close();
  52.         cout << "Количество повторений: " << count << endl;
  53.     }
  54.    
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement