In: Computer Science
In Java
A palindrome is a word or sequence of characters which reads the same backward and forward, such as madam, dad, racecar, 5885. In java Write a program that asks user to enter a word and prints if the word is a palindrome or not.
Palindrome.java
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
// TODO Auto-generated method
stub
Scanner scan = new
Scanner(System.in);
System.out.println("************************* Checking palindrome
condition ********************");
System.out.println("Enter a word :
");
String word = scan.next();
boolean flag =
isPalindrome(word);
if(flag)
System.out.println(word+" is Palindrome.");
else
System.out.println(word+" is not palindrome.");
}
private static boolean isPalindrome(String word)
{
// TODO Auto-generated method
stub
int i=0;
int j=word.length()-1;
boolean flag = true;
while(i<=j) {
if(word.charAt(i)==word.charAt(j)) {
i++;
j--;
continue;
}
else {
flag =false;
break;
}
}
return flag;
}
}
Output
************************* Checking palindrome condition
********************
Enter a word :
madam
madam is Palindrome.
Screenshot
Feel free to ask any doubts, if you face any difficulty in understanding.
Please upvote the answer if you find it
helpful