Palindrome.java
Question:
Write an application that determines whether a phrase entered by the user is a palindrome. A palindrome is a phrase that reads the same backward and forward without regarding capitalization or punctuation. For example, “Dot saw I was Tod”, “Was it a car or a cat I saw”, and “Madam Im Adam” are palindromes. Display the appropriate feedback: You entered a palindrome or You did not enter a palindrome.
CODE:
import java.util.*;
public class Palindrome {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter phrase : ");
String phrase = sc.nextLine();
phrase = phrase.replace(" ", "").toLowerCase();
phrase = phrase.replaceAll("[^a-zA-Z0-9]", "");
int i = 0, j = phrase.length() - 1;
boolean ispalindrome= true;
while (i < j) {
if (phrase.charAt(i) != phrase.charAt(j)) {
ispalindrome = false;
}
i++;
j--;
}
if(ispalindrome)
{
System.out.println("you entered a palindrome.");
}
else
{
System.out.println("you did not enter a palindrome.");
}
}
}
RESULTS:
50% good
pls help