Advertisement
Guest User

Untitled

a guest
Nov 29th, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     setlocale(LC_ALL, ".1251");
  10.     ifstream in("in.txt");
  11.     ofstream out("out.txt");
  12.     const int nmax = 500;
  13.     char *str = new char[nmax];
  14.     char *w1, *w2;
  15.     bool p1 = true, p2 = true;
  16.     string word1, word2, s = "";
  17.     int i = 0, n, k;
  18.     in.getline(str, 500, '\n');
  19.     if (str == NULL)
  20.     {
  21.         cout << "Пустой файл." << endl;
  22.         return 1;
  23.     }
  24.     if (in.eof() != NULL)
  25.     {
  26.         cout << "Файл содержит только слова для поиска." << endl;
  27.         return 1;
  28.     }
  29.     if ((w1 = strtok(str, " ")) == NULL)
  30.     {
  31.         cout << "В первой строке нет слов для поиска." << endl;
  32.         return 1;
  33.     }
  34.     if ((w2 = strtok(NULL, " ")) == NULL)
  35.     {
  36.         cout << "Файл содержит только одно слово для поиска." << endl;
  37.         return 1;
  38.     }
  39.     cout << "Первое слово:" << w1 << endl;
  40.     cout << "Второе слово:" << w2 << endl;
  41.     word1 = w1;
  42.     word2 = w2;
  43.     while (!in.eof())//пока не конец файла
  44.     {
  45.         in.getline(str, 500);
  46.         s += str;
  47.         s += "\n";
  48.     }
  49.     int f = s.find(word1);
  50.     int l = s.rfind(word2);
  51.     if (l != -1 && f != -1)
  52.     {
  53.         //if (
  54.         //  ((f == 0 || s[f - 1] == ' ' || s[f - 1] == '\n') && (s[f + s.length()] == ' ' || s[f + s.length()] == '\n'))
  55.         //  &&
  56.         //  ((s[l - 1] == ' ' || s[l - 1] == '\n')  && (s.length() == l + word2.length() ||  s[l + word2.length()] == ' ' || s[l + word2.length()] == '\n'))
  57.         //  )
  58.         //{
  59.             s.erase(f, word1.length());
  60.             s.insert(f, word2);
  61.             s.erase(l, word2.length());
  62.             s.insert(l, word1);
  63.         //}
  64.     }
  65.     cout << s;
  66.     //if (l == 0)
  67.     //  cout << "Таких слов в тексте нет." << endl;
  68.     in.close();
  69.     out.close();
  70.  
  71.  
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement