C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Palindrome Program - Java Programs

Write a program using a method palin(), to check whether a string is a palindrome or not. A palindrome is a string that reads the same from left to right and vice-versa.
Example: MADAM, ARORA, ABBA

import java.io.*; class Palindrome{ public static void main(String args[])throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("String: "); String s = br.readLine(); if(palin(s)) System.out.println(s + " is palindrome."); else System.out.println(s + " is NOT palindrome."); } public static boolean palin(String w){ String rev = ""; for(int i = w.length() - 1; i >= 0; i--) rev += w.charAt(i); return w.equalsIgnoreCase(rev); } }

Output

 
 OUTPUT : 
String: MADAM
MADAM is palindrome.