I appreciate any help with the following question. Write a Java program that use
ID: 3841733 • Letter: I
Question
I appreciate any help with the following question.
Write a Java program that uses a stack to test whether an input string is a palindrome. To implement the solution to this problem, use the stack of characters from the program below.
public class Stack
{
public Stack()
{
size = 100;
list = new char[size];
n = 0;
}
public Stack(int s)
{
size = s;
list = new char[size];
n = 0;
}
public void push(char c)
{
if (n<size)
{
list[n] = c;
n++;
}
else
System.out.println ("error: Stack is full ");
}
public void pop()
{
if (n>0)
n--;
else
System.out.println ("error: Stack is empty. ");
}
public char peek()
{
if (n>0)
return list [n - 1];
else
{
System.out.println ("error: Cannot peek. Stack is empty. ");
return ' ';
}
}
public boolean isEmpty()
{
return n == 0;
}
private char[] list;
private int size;
private int n;
}
public class Main
{
public static void main(String[] args)
{
Stack s = new Stack(9);
System.out.println("Insertion of 10 characters in s");
for (int i = 0; i < 10; i++)
{
char x = (char) (32 + (int) (Math.random() * 95));
System.out.println(x + " --> " + (char) x);
s.push(x);
}
System.out.println(" Displaying, deleting elements");
for (int i = 0; i < 10; i++)
{
System.out.println("Item at the top: " + s.peek());
s.pop();
}
}
}
Explanation / Answer
public class Stack {
private char[] list;
private int size;
private int n;
public Stack()
{
size = 100;
list = new char[size];
n = 0;
}
public Stack(int s)
{
size = s;
list = new char[size];
n = 0;
}
public void push(char c)
{
if (n<size)
{
list[n] = c;
n++;
}
else
System.out.println ("error: Stack is full ");
}
public void pop()
{
if (n>0)
n--;
else
System.out.println ("error: Stack is empty. ");
}
public boolean isEmpty()
{
if(n==0)
return true;
else
return false;
}
public char peek()
{
if (n>0)
return list [n - 1];
else
{
System.out.println ("error: Cannot peek. Stack is empty. ");
return ' ';
}
}
}
Stackmain.java
import java.util.*;
public class Stackmain {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("enter number of charecters:");
int limit=sc.nextInt();
char inarr[]=new char[limit];
char outarr[]=new char[limit];
Stack s=new Stack(limit);
for(int i=0;i<limit;i++){
char x = (char) ( 32+ (int) (Math.random() *95));
//System.out.println("enter charcter:");
//char x=sc.next().charAt(0);
s.push(x);
inarr[i]=x;
System.out.print("inserted element is:"+x);
}
System.out.println(" Displaying, deleting elements");
for (int i = 0; i < limit; i++)
{
char rm=s.peek();
outarr[i]=rm;
System.out.println("Item at the top: " + rm);
s.pop();
}
boolean ispallendrome= chkpallendrome(inarr,outarr);
if(ispallendrome)
System.out.println("IT IS PALLENDROME");
else
System.out.println("NOT A PALLENDROME:");
}
public static boolean chkpallendrome(char inarr[],char outarr[]){
boolean flag=true;
for(int i=0;i<inarr.length;i++){
if(inarr[i]==outarr[i]){
flag=true;
}else
{
flag=false;
break;
}
}
if(flag==true)
return true;
else
return false;
}
}
output:
enter number of charecters:
6
inserted element is:Uinserted element is:=inserted element is:]inserted element is:Ainserted element is:Ginserted element is:Y
Displaying, deleting elements
Item at the top: Y
Item at the top: G
Item at the top: A
Item at the top: ]
Item at the top: =
Item at the top: U
NOT A PALLENDROME:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.