Check Palindrome String in Java

This is a Java Program to Check whether a String is a Palindrome. A palindrome string is a string that is same after reverse.
Here is the source code of the Java Program to Check whether a String is a Palindrome.
package com.javaguides.collections.utils;

import java.util.Scanner;

public class CheckPalindrome
{
   public static void main(String args[])
   {
      String str, rev = "";
      Scanner sc = new Scanner(System.in);
 
      System.out.println("Enter a string: ");
      
      str = sc.nextLine();
 
      int length = str.length();
 
      for ( int i = length - 1; i >= 0; i-- ) {
         rev = rev + str.charAt(i);
      }
 
      if (str.equals(rev)) {
         System.out.println(str+" is a palindrome");
      }
      else {
         System.out.println(str+" is not a palindrome");
      }
      
      sc.close();
 
   }
}

Output

Enter a string: 
radar

radar is a palindrome

Comments