Please use the problem solving process for this question. According to Wikipedia
ID: 3763920 • Letter: P
Question
Please use the problem solving process for this question. According to Wikipedia' a palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or reverse direction. Famous examples include "Amore, Roma", "A man, a plan, a canal: Panama" and “ZEROREZ". Problem. Write an application, isPalindrome, in Java that will take a string input from a user and determine whether or not the string is a palindrome. Input: String Output: Original String, Reverse String, Whether or Not a palindrome Assumptions: Ignore spaces and punctuation Additional Requirement: Use at least one Exception Handling Mechanism.
Explanation / Answer
import java.util.Scanner;
/**
* @author Srinivas
*
*/
public class PalindromeCheck {
public static void main(String args[]) {
String inputString;
Scanner in = new Scanner(System.in);
System.out.print("Input a string :");
inputString = in.nextLine();
String reversedString = isPalindrome(inputString);
System.out.println("Original String: " + inputString);
System.out.println("Reverse String: " + reversedString);
inputString = inputString.toLowerCase().replaceAll("\W", "");
if (inputString.equalsIgnoreCase(reversedString)) {
System.out.println("Palindrome");
} else {
System.out.println("Not a Palindrome");
}
}
/**
* @param inputString
* @return reverse of a given string
*/
public static String isPalindrome(String inputString) {
boolean flag = true;
String reversedString = "";
inputString = inputString.toLowerCase().replaceAll("\W", "");
int length = inputString.length();
for (int i = length - 1; i >= 0; i--)
reversedString = reversedString + inputString.charAt(i);
// System.out.println("Reverse of entered string is: "+reverse);
return reversedString;
}
}
OUTPUT :
Test1:
Input a string :Amore, Roma
Original String: Amore, Roma
Reverse String: amoreroma
Palindrome
Test2:
Input a string :A man, a plan, a canal: Panama
Original String: A man, a plan, a canal: Panama
Reverse String: amanaplanacanalpanama
Palindrome
Test3:
Input a string :ZEROREZ
Original String: ZEROREZ
Reverse String: zerorez
Palindrome
Test4:
Input a string :srinivas
Original String: srinivas
Reverse String: savinirs
Not a Palindrome