Vlad5080

search of symbols

Jan 16th, 2020
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. int Str_check(string Stroka, string Need) {
  8.     string word;
  9.     stringstream x;
  10.  
  11.     x << Stroka;
  12.     int i = 0;
  13.     while (x >> word) {
  14.         if (word == Need) {
  15.             i++;
  16.         }
  17.     }
  18.     return i;
  19. }
  20.  
  21. int main() {
  22.     setlocale(0, "rus");
  23.  
  24.     int i = 0;
  25.     fstream f("file.txt");
  26.     string need, stroka;
  27.     char symbol;
  28.     bool s = false;
  29.  
  30.     cout << "Введите символ/слово: \n -> ";
  31.     cin >> need;
  32.  
  33.     if (need.length() == 1) {
  34.         symbol = need[0];
  35.         s = true;
  36.     }
  37.    
  38.  
  39.     if (s) {
  40.         char ch;
  41.         while (!f.eof()) {
  42.             ch = f.get();
  43.             if (ch == symbol)
  44.                 i++;
  45.         }
  46.     }
  47.     else {
  48.         while (1) {
  49.             getline(f, stroka);
  50.             i = i + Str_check(stroka, need);
  51.             if (f.eof())
  52.                 break;
  53.         }
  54.     }
  55.     f.close();
  56.     cout << i << endl;
  57.  
  58.     system("pause");
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment