Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- setlocale(LC_ALL, ".1251");
- ifstream in("in.txt");
- ofstream out("out.txt");
- const int nmax = 500;
- char *str = new char[nmax];
- char *w1, *w2;
- bool p1 = true, p2 = true;
- string word1, word2, s = "";
- int i = 0, n, k;
- in.getline(str, 500, '\n');
- if (str == NULL)
- {
- cout << "Пустой файл." << endl;
- return 1;
- }
- if (in.eof() != NULL)
- {
- cout << "Файл содержит только слова для поиска." << endl;
- return 1;
- }
- if ((w1 = strtok(str, " ")) == NULL)
- {
- cout << "В первой строке нет слов для поиска." << endl;
- return 1;
- }
- if ((w2 = strtok(NULL, " ")) == NULL)
- {
- cout << "Файл содержит только одно слово для поиска." << endl;
- return 1;
- }
- cout << "Первое слово:" << w1 << endl;
- cout << "Второе слово:" << w2 << endl;
- word1 = w1;
- word2 = w2;
- while (!in.eof())//пока не конец файла
- {
- in.getline(str, 500);
- s += str;
- s += "\n";
- }
- int f = s.find(word1);
- int l = s.rfind(word2);
- if (l != -1 && f != -1)
- {
- //if (
- // ((f == 0 || s[f - 1] == ' ' || s[f - 1] == '\n') && (s[f + s.length()] == ' ' || s[f + s.length()] == '\n'))
- // &&
- // ((s[l - 1] == ' ' || s[l - 1] == '\n') && (s.length() == l + word2.length() || s[l + word2.length()] == ' ' || s[l + word2.length()] == '\n'))
- // )
- //{
- s.erase(f, word1.length());
- s.insert(f, word2);
- s.erase(l, word2.length());
- s.insert(l, word1);
- //}
- }
- cout << s;
- //if (l == 0)
- // cout << "Таких слов в тексте нет." << endl;
- in.close();
- out.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement