Pin It

How to check whether a string is a Palindrome or not through a Java program ?.

Program demonstrating simple Palindrome test for a string through a simple Java code.

package com.hubberspot.string.example;

import java.util.Scanner;

public class PalindromeTest {

  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.print("Enter a string to test : ");
    String string = input.nextLine();

    if (palindromeCheck(string))
      System.out.println(string + " is a palindrome");
    else
      System.out.println(string + " is not a palindrome");
    }

  public static boolean palindromeCheck(String string) {

    int low = 0;

    int high = string.length() - 1;

    while (low < high) {
       if (string.charAt(low) != string.charAt(high))
   return false; 

       low++;
       high--;
    }

    return true; 
  }
}

Output of the program : 


 




Video tutorial to demonstrate how to check whether a given string is palindrome or not through a Java program.








Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot