Java 2211 Palindrome Tester Write Program Uses Stack Determine Whether String Palindrome E Q37132763

(In Java) 22.11 (Palindrome Tester) Write a program that uses astack to determine whether a string is a palindrome (i.e., thestring is spelled identically backward and forward). The programshould ignore spaces and punctuation.


Answer


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

int top = -1, front = 0;
int stack[50];

void push(char a)
{
top++;
stack[top] = a;
}

void pop()
{
top–;
}

void main()
{
int i;
char s[50], b;
clrscr();
printf(“nEnter Stringn”);
gets(s);
for(i = 0;s[i] != ”;i++)
{
if(s[i]==’ ‘)
continue;
b = s[i];
push(b);
}
for (i = 0;i<(strlen(s)/2);i++)
{
if(stack[top] == stack[front])
{
pop();
front++;
}
else if(stack[top]==’ ‘ || stack[top]==’,’ || stack[top]==’.’ ||stack[top]==’?’ || stack[top]==’!’ || stack[top]==’-‘ )
continue;
else
{
printf(“%s is not a palindromen”, s);
break;
}
}
if ((strlen(s) / 2) == front)
printf(“%s is palindromen”, s);
getch();
}

Enter String sajal,la jas sajal,lajas is palindrome

Enter String sajal la jas sajal lajas is palindrome

Enter Str ing saja l gupta sajal gupta is not a pa l indrome

Enter

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.