Answer to Question #215870 in C++ for this pc

Question #215870
You are given a file words.txt containing some words. First, you have to find out the number of
words in the file. Read the words into a 2D array. You are required to make each row a
palindrome by adding additional character(s) if required. Write the updated array in another
file paliandrome.txt
1
Expert's answer
2021-07-12T00:17:58-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS