168,364 views
7 votes
7 votes
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

User Novie
by
3.1k points

1 Answer

25 votes
25 votes
Ok, where is the phrase that we are trying to determine to see if it’s a palindrome. I can help you with this, I just need the provided phrase that was given with the question as well. All I see is transcripts below
User Daveeloo
by
2.9k points