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

java progrom use only one String and push n/2 to test whether an input string is

ID: 3557122 • Letter: J

Question

java progrom use only one String and push n/2 to test whether an input string is palindrome???plz come with the output

here is palindrome program i had, but didnt have PUSH n/2

import java.util.Stack;
import java.util.Scanner;

public class PalindromeTest {

public static void main(String[] args) {

   System.out.print("Enter any string:");
Scanner in=new Scanner(System.in);
String inputString = in.nextLine();
Stack stack = new Stack();

for (int i = 0; i < inputString.length(); i++) {
stack.push(inputString.charAt(i));
}

String reverseString = "";

while (!stack.isEmpty()) {
reverseString = reverseString+stack.pop();
}

if (inputString.equals(reverseString))
System.out.println("The input String is a palindrome.");
else
System.out.println("The input String is not a palindrome.");

}
}

Explanation / Answer

Note:

The sample code given in the question uses stacks. However, there is nowhere mentioned in the question that stacks must be used to develop this program. Hence, the below program is an implementation of the question without using stacks.

Program Code:

import java.util.Scanner;
import java.util.Stack;
class PalindromeTest
{
   public static void main(String args[])
   {
       Scanner sc=new Scanner(System.in);
         String str="Madam";
         System.out.println("Enter the String to be checked for palindrome:");
         str=sc.next();
         isPalindrome(str);
   }
    @SuppressWarnings("unchecked")
   public static void isPalindrome(String str)
    {
        int len=str.length();
        int half=len/2;
        boolean pali=true;
        Stack stack = new Stack();
        for(int i=0; i <= half; i++)
        {
            if(Character.toUpperCase((char) stack.push(str.charAt(i)))!=Character.toUpperCase(str.charAt(len-i-1)))
            {
                pali=false;
                break;
            }
        }
        if(pali==true)
           System.out.println("The given string is palindrome!");
        else
           System.out.println("The given string is not palindrome!");
    }

}

Output:

Enter the String to be checked for palindrome:
hissih
The given string is palindrome!