×

Java Programs

Java Practice

Easiest way to check Given String is Palindrome String or not in Java

This java program will read a string and check whether it is Palindrome or not using easiest method.

Java program to check Given String is Palindrome String or not

package com.includehelp.stringsample;

import java.util.Scanner;

/**
 * Easiest way to check Given String is Palindrome String or not
 * @author includehelp
 */
public class PalindromString {
    
    static boolean isPalindromString(String inputStr){
        StringBuilder sb  = new StringBuilder(inputStr);
        String reverseStr = sb.reverse().toString();
 
        return (inputStr.equalsIgnoreCase(reverseStr));              
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter String : ");
        String inString = sc.next();
        
        if(isPalindromString(inString)){
            System.out.println(inString +" is a Palindrom String");
        }
        else{
            System.out.println(inString +" is not a Palindrom String");
        }
    }
}

Output

First run:
Enter String : india
india is not a Palindrom String

Second run:
Enter String : abcba
abcba is a Palindrom String

Java String Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.