Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now