Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Program in C++ using vectors A word palindrome reads the same backwards

ID: 3555087 • Letter: W

Question

Write a Program in C++ using vectors

A word palindrome reads the same backwards as forwards, if you take the unit to be a word. For example, BORES ARE PEOPLE THAT SAY THAT PEOPLE ARE BORES, in this assignment use a vector of strings. Read the words from a file until you come to the end of a line. Store each word as one of the elements of a vector. Thus in the example, from the first line of the file, you would store in the vectors [0] position the string so and in the vectors [1] position the string PATIENT and in the vectors [2] positioning the string A and in the vectors [3] positioning the string DOCTOR and so forth. Do not declare the vector with the number of positions to be reserved at the outset. Instead use push_back and later .size() to find out how positions were needed.

Then have your program call a function into which you pass the vector and its size, and have the function determine if you have a word palindrome . Pass back to the main program a bool variable that is true if you do have a word palindrome , and false if you do not . Then in the main program, print out the line of words and a statement saying wheter it is a word palindrome.

To make the task a little easier, the words in the file are all in capitals, but each line does contain punctuation. Do not change what is in the file. Instead, write your program general enough to handle it.

Use a text file named Palindrome.txt which contains the following information:

THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.

YOU CAN CAGE A SWALLOW, CANT YOU, BUT YOU CANT SWALLOW A CAGE, CAN YOU? BORES ARE PEOPLE THAT SAY THAT PEOPLE ARE BORES.

EARLY TO BED AND EARLY TO RISE MAKES A MAN HEALTHY, WEALTHY, AND WISE.

DOES MILK MACHINERY MILK DOES? GOD KNOWS MAN.

WHAT IS DOUBTFUL IS WHAT MAN KNOWS GOD.

LEATHERY SKIN IS SKIN LEATHER.

HIDE! HIDE! THE COWS OUTSIDE! YOU HUNT WOLVES; WOLVES HUNT YOU.

SO PATIENT A DOCTOR TO DOCTOR A PATIENT SO.

BEAUTY IS AS BEAUTY DOES.

Explanation / Answer

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <vector>

using namespace std;

bool check_palindrome(vector<string>& random)
{
int siz=random.size();
for (unsigned i=0; i<siz/2; i++)
{
string temp=random[i];
string temp1=random[siz-1-i];
int j=0;
for(int j=0;j<temp.size();j++)
{
if(temp[j]=='' && temp1[j]=='')
return true;
if(temp[j]!=temp1[j])
return false;
}

}
return true;
}

int main()
{
string STRING;
   ifstream infile;
   infile.open ("Palindrome.txt");
while(!infile.eof()) // To get you all the lines.
{
getline(infile,STRING); // Saves the line in STRING.
cout<<STRING<<endl; // Prints our STRING.

string buf; // Have a buffer string
stringstream ss(STRING); // Insert the string into a stream
vector<string> tokens; // Create vector to hold our words

while (ss >> buf)
{
for(int i=0;i<buf.size();i++)
{
if(buf[i]=='.' || buf[i]==',' || buf[i]==';' || buf[i]=='!' || buf[i]=='?' || buf[i]==':')
buf[i]='';
}
//if(buf[i]?

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote