A palindrome is any word, phrase, or sentence that reads the same forward and ba
ID: 3675080 • Letter: A
Question
A palindrome is any word, phrase, or sentence that reads the same forward and backward. For example: Able was I ere I saw Elba A man A plan a canal Panama Desserts I stressed Kayak abcde edcba Write a program that uses a stack to determine if a string of characters is a palindrome. This program will ask the user to enter a string, then printout a statement indicating whether or not the string was a palindrome. You may use any data structure you wish to create the stack so long as the stack is implemented and used correctly. Name your demo class (containing the main method) PalindromeDemo.
Explanation / Answer
#include<iostream.h>
#include<string.h>
char stack[100];
int top=-1;
void popC();
void pushC(char);
int main()
{
char st[100];
int counter=0;
cout<<"enter string";
cin>>str;
int length= strlen(st);
for(int i=0;i<length;i++)
{
pushC(st[i]);
}
for(int i=0;i<length;i++)
{
if(st[i] == pop())
counter++;
}
if(counter == length)
cout<<"String entered is palindrome";
else
cout<<"String entered is not palindrome";
return 0;
}
void pushC(char c)
{
stack[++top]=c;
}
char pop()
{
return(stack[top--]);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.