Java Examples

simple java program palindrome string

Table of Contents

Palindrome.java

import java.util.Scanner;

/**
 * A word reads the same forward and backward is a palindrome. 
 * @author Candid Java
 */
public class Palindrome {

 public static void main(String args[]) {
  String a, b = "";
  Scanner s = new Scanner(System.in);
  System.out.print("Enter the string :");
  a = s.nextLine();
  int n = a.length();
  for (int i = n - 1; i >= 0; i--) {
   b = b + a.charAt(i);
  }
  if (a.equalsIgnoreCase(b)) {
   System.out.println("The string is palindrome.");
  } else {
   System.out.println("The string is not palindrome.");
  }
 }
}

Output

Enter the string :racecar
The string is palindrome.