#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
bool check_palindrom(string arg)
{
string reverse = "";
for (int i = arg.size() - 1; i >= 0; i--)
{
reverse += arg[i];
}
if (arg== reverse)
{
return true;
}
else
{
return false;
}
}
string get_palindrom(string arg)
{
for (int i = 0; i < arg.size(); i++)
{
if (check_palindrom(arg.substr(i)))
{
string result = arg;
for (int j = i - 1; j >= 0; j--)
{
result += arg[j];
}
return result;
}
}
}
int main()
{
const string file_input = "word.txt";
const string file_output = "palindrom.txt";
ifstream inp(file_input);
if (!inp)
{
cout << "File read error " << file_input << endl;
return -1;
}
vector <string> words;
while (inp)
{
string str_input;
inp >> str_input;
words.push_back(str_input);
}
for (int i = 0; i < words.size()-1; i++)
{
words[i] = get_palindrom(words[i]);
}
ofstream out(file_output);
for (int i = 0; i < words.size() - 1; i++)
{
out<<words[i]<< "\n";
}
return 0;
}
Comments
Leave a comment