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

Write a program that will recognize palindromes with the help of a stack. A pali

ID: 3625598 • Letter: W

Question

Write a program that will recognize palindromes with the help of a stack. A palindrome is a strting that can be read the same way from the left and from the right. For example: "ABBA" is a palindrome. If read left-to-right it spells: A-B-B-A. if right-to-left, it still spells: A-B-B-A. The way to recognize palindromes is to feed a string into a stack, pop the whole sting off the stack, and compare the original string with the one that comes out of the stack. For example, consider a string with 4 characters original string = char1 char2 char3 char4 when put on the stack, a LIFO structure, the string becomes: new string = char4 char3 char2 char1 You are to implement a stack structure. if you cannot implement the stack by yourselves, you may use the standard Template Library stack. Your stack should accommodate strings of ANY size Your program should recieve a list of strings from a file called "input.txt". and should output the results of each comparision to a fie called "output.txt".

Explanation / Answer

please rate - thanks

#include<iostream>
#include <fstream>
using namespace std;
char pop(char[], int& );
void push(char[],int&,int, char) ;
bool checkpalin(string,int,char[]);
int main()
{ int maxsize=20;
char stack[maxsize];
string input;
bool good;
ifstream in;
ofstream out;
in.open("input.txt");       
if(in.fail())           
   { cout<<"input file did not open please check it ";
   system("pause");
   return 1;
   }
out.open("output.txt");        
in>>input;
while(in)
{
good=checkpalin(input,maxsize,stack);
if(good)
    out<<"YES ";
else
     out<<"NO ";
in>>input;
     }
out.close();
in.close();
return 0;
}    

bool checkpalin(string input,int maxsize,char stack[])
{char c;
int n=0,i=0;
while(i<input.length())
{ push(stack,n,maxsize,input[i]);
i++;
}
i=0;
while(i<input.size())
   {c=pop(stack,n);
    if(c!=input[i++])
        return false;
   }  
return true;    
  
}   
char pop(char a[], int& n)
{ if (n <= 0)
        {cout<<"Stackunderflow - program aborted ";
          system("pause");
          system("exit");
          }
   n--;
   return a[n];
}
void push(char a[], int& n, int maxsize, char val)
{if (n >= maxsize)
        {cout<<"Stack overflow - program aborted ";
          system("pause");
          system("exit");
          }
   a[n] = val;
   n++;
   return;
}

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