Dremendo Tag Line

Input word and check palindrome word or not in Java

String - Question 3

In this question, we will see how to input a word and check if it is a palindrome word or not in Java programming. To know more about string click on the string lesson.

Q3) Write a program in Java to input a word and check if it is a palindrome word or not.

Program

import java.util.Scanner;

public class Q3
{
    public static void main(String args[])
    {
        String s,rs="";
        int i;
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a word");
        s=sc.nextLine();

        for(i=s.length()-1; i>=0; i--)
        {
            rs=rs+s.charAt(i);
        }

        if(s.compareToIgnoreCase(rs)==0)
        {
            System.out.println("Palindrome word");
        }
        else
        {
            System.out.println("Not palindrome word");
        }
    }
}

Output

Enter a word
Madam
Palindrome word
video-poster