• Предмет: Информатика
  • Автор: rubin12345
  • Вопрос задан 3 месяца назад

1. Найти в файлі f всі слова, які зліва направо і навпаки читаються однаково і скласти із них новий файл q.
с++

Ответы

Ответ дал: microtima
1

Ответ:

Here is a sample code in C++ that does the job of finding all the words in a file "f" that are palindromes and creating a new file "q" consisting of those words:

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

bool is_palindrome(string word) {

   int n = word.length();

   for (int i = 0; i < n / 2; i++)

       if (word[i] != word[n - i - 1])

           return false;

   return true;

}

int main() {

   ifstream fin("f.txt");

   ofstream fout("q.txt");

   string word;

   while (fin >> word) {

       if (is_palindrome(word))

           fout << word << endl;

   }

   fin.close();

   fout.close();

   return 0;

}

This code uses the ifstream class to read the input file f.txt and the ofstream class to create the output file q.txt. The is_palindrome function is used to determine whether a given word is a palindrome or not. The code reads the file word by word using the fin >> word line and for each word, it checks if it's a palindrome using the is_palindrome function. If a word is found to be a palindrome, it is written to the q.txt file. Finally, both the input and output files are closed.


rubin12345: Thank you
Вас заинтересует