Write a program to input a word and check whether palindrome word or not.
Eg NITIN,MOM,DAD are palindrome word
Answers
Answer:
import java.util.Scanner;
class palindrome
{
static String reverse(String s)
{
char[] letters = new char[s.length()];
int ind = 0;
for(int i = s.length() - 1; i >= 0; i--)
{
letters[ind] = s.charAt(i);
ind++;
}
String reversed = "";
for(int i = 0; i < s.length(); i++)
{
reversed = reversed + letters[i];
}
return reversed;
}
static boolean isPalindrome(String str)
{
if(reverse(str).equals(str))
{
return true;
}
else
{
return false;
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string:");
String x = sc.nextLine();
if(isPalindrome(x))
{
System.out.println(x + " is a palindrome");
}
else
{
System.out.println(x + " is not a palindrome");
}
}
}
Explanation: