Dremendo Tag Line

Input a word and check if it is palindrome word or not using a function in Java

Function - Question 4

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

Q4) Write a program in Java to input a word and check if it is palindrome word or not using a function. The function should return 1 if the word is palindrome else return 0.

A word is called palindrome if its reverse form is equal the original word.

Example: madam
Reverse Form: madam

Program

import java.util.Scanner;

public class Q4
{
    public static int palindrome(String str)
    {
        String rev="";
        int i;

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

        if(str.compareToIgnoreCase(rev)==0)
        {
            return 1;
        }
        return 0;
    }

    public static void main(String args[])
    {
        String s;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter a String: ");
        s=sc.nextLine();
        if(palindrome(s)==1)
        {
            System.out.print("Palindrome Word");
        }
        else
        {
        	System.out.print("Not Palindrome Word");
        }
    }
}

Output

Enter a String: madam
Palindrome Word
video-poster