A palindrome is a word or a phrase that is the same when read both forward and backward. Examples are: "bob," "sees," or "never odd or even" (ignoring spaces). Write a program whose input is a word or phrase, and that outputs whether the input is a palindrome. Ex: If the input is: bob the output is: bob is a palindrome The code needs to be in Java.
Java code
import java.util.*;
public class Palindrome
{ public static void main(String[] args)
{ String input, reverse_input = "";
Scanner in = new Scanner(System.in);
System.out.print("Enter a word/phrase : ");
input = in.nextLine();
int length = input.length();
for (int l=length-1;l>=0;l--)
reverse_input = reverse_input + input.charAt(l);
if (input.equals(reverse_input))
System.out.println("Entered word/phrase ("+input+") is a palindrome");
else
System.out.println("Entered word/phrase ("+input+") is not a palindrome");
}
}
Output :
Enter a word/phrase : neveroddoreven
Entered word/phrase (neveroddoreven) is a palindrome
Get Answers For Free
Most questions answered within 1 hours.