Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

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.








 
© 2021 Learn Java by Examples Template by Hubberspot