Advertisement
andruhovski

C++ string demo

Feb 6th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <fstream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. bool equal(const string& cw, const string& w) {
  6.     char punct[] = {'.',',','?','!'};
  7.  
  8.     if (cw == w) return true;
  9.     for (int i=0; i < sizeof(punct); ++i)
  10.         if (cw == w + punct[i]) return true;
  11.     return false;
  12. }
  13.  
  14. int main() {
  15.     string word, curword;
  16.  
  17.     cout << " Введите слово для поиска: ";
  18.     cin >> word;
  19.  
  20.     ifstream fin("infile.txt");
  21.     if (!fin) { cout << "Ошибка открытия файла." << endl; return 1; }
  22.  
  23.     int count = 0;
  24.     while (!fin.eof()) {
  25.         fin >> curword;
  26.         if (equal(curword, word)) count++;
  27.     }
  28.     cout << "Количество вхождений слова: " << count << endl;
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement