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

Have to create the stack and this is for java. Write a program in Java to test f

ID: 3783402 • Letter: H

Question

Have to create the stack and this is for java. Write a program in Java to test for palindromes using a stack. You are not allowed to use Java API Stack classes for this problem. A palindrome is a string that reads the same way forwards and backwards. Your program should do the following steps.
1.   Prompt the user at command line to enter a string.
2.   Convert the input string to lower case.
3.   Push each letter character of the string on the stack until the string ends. Do not push blank spaces, special characters or punctuation marks on the stack. To check for letter characters in the input string, you can cast the character you extract from the input string to an int to get its ASCII value. Then you can check if its ASCII value lies between 97 (lower case 'a') and 122 (lower case 'z').
4.   When the string ends, pop the entire stack and store it in another string. This is the reverse string of what you input.
5.   Compare the letters (only) of the input string and the reversed string you generated in step 3 above. If they are equal, the input string is a palindrome.
6.   Print if the string is a palindrome or not.
Some sample inputs are shown below:
Enter a string to test for palindrome: Tuna roll or a nut?
Reverse string is: tunarolloranut
Input string is a palindrome

Enter a string to test for palindrome: Race fast safe car!
Reverse string is: racefastsafecar
Input string is a palindrome

Enter a string to test for palindrome: Race car.
Reverse string is: racecar
Input string is a palindrome.

Enter a string to test for palindrome: Fast car.
Reverse string is: cartsaf
Input string is not a palindrome.

Explanation / Answer

//Tested on Eclipse

/*******************Stack.java*************************/


public class Stack {
   protected char arr[];
protected int top, size, len;
/* Constructor for arrayStack */
public Stack(int n)
{
size = n;
len = 0;
arr = new char[size];
top = -1;
}
/* Function to check if stack is empty */
public boolean isEmpty()
{
return top == -1;
}
/* Function to check if stack is full */
public boolean isFull()
{
return top == size -1 ;
}
/* Function to get the size of the stack */
public int getSize()
{
return len ;
}
/* Function to check the top element of the stack */
public int peek()
{
if( isEmpty() )
System.out.println("Underflow Exception");
return arr[top];
}
/* Function to add an element to the stack */
public void push(char val)
{
if(top + 1 >= size)
System.out.println("Overflow Exception");
if(top + 1 < size )
arr[++top] = val;
len++ ;
}
/* Function to delete an element from the stack */
public char pop()
{
if( isEmpty() )
System.out.println("Underflow Exception");
len-- ;
return arr[top--];
}
/* Function to compare value of stack and String br are palindrome or not */
public void palindrome(String br)
{
if (len == 0)
{
System.out.print("Empty ");
return ;
}
int c=0;
for (int i = top; i >= 0; i--){
   /*If any char is diff then it is not palindrome*/
   if(br.charAt(c)!=arr[i]){
System.out.print("Input string is not a palindrome.");
return;
   }
   c++;
}
System.out.println("Input string is a palindrome. ");
}
}

/*******************StringReverse.java***************/

import java.util.Scanner;


public class StringReverse {

  
   public static void main(String[] args) {
       /*StringBuilder object creation*/
       StringBuilder br=new StringBuilder();
       /*Scanner for input*/
       Scanner input=new Scanner(System.in);
       System.out.println("Enter a string to test for palindrome: ");
       String str=input.nextLine();
       /*Creating Stack class object of input String length*/
       Stack stack=new Stack(str.length());
       /*Array of char of lower case*/
       char arr[]=str.toLowerCase().toCharArray();
      
       for(char val:arr){
           int ascii=(int)val;
           /*Pushing element into stack only lower case character*/
           if(ascii>=97&&ascii<=122){
               br.append(val);
               stack.push(val);
           }
       }
       String org=br.toString();
       System.out.println("Reverse string is: "+br.reverse());
       stack.palindrome(org);

   }

}

/*****************output on different input***************/

Enter a string to test for palindrome:
Tuna roll or a nut?
Reverse string is: tunarolloranut
Input string is a palindrome.

Enter a string to test for palindrome:
Race fast safe car!
Reverse string is: racefastsafecar
Input string is a palindrome.

Enter a string to test for palindrome:
Race car.
Reverse string is: racecar
Input string is a palindrome.

Enter a string to test for palindrome:
Fast car.
Reverse string is: ractsaf
Input string is not a palindrome.

Thanks a lot

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