This is in java and communt is needed. A palindrome is any word, phrase, or sent
ID: 3677793 • Letter: T
Question
This is in java and communt is needed.
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
PalindromeDemo.java
package org.students;
import java.util.Stack;
import java.util.Scanner;
class PalindromeDemo{
public static void main(String[] args) {
System.out.print("Enter any string:");
Scanner in=new Scanner(System.in);
String str=in.nextLine();
String inputString =str.toLowerCase();
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 String is a palindrome **");
else
System.out.println("** The input String is not a palindrome **");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.