• Register
Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Write a method to check if input String is Palindrome?

+1 vote
630 views

Write a method to check if input String is Palindrome?

How to check string is Palindrome not? I want to check a string is a palindrome or not. Write an easy method to check inputed string is palindrome or not.

Example :- String str = "MOM";

                  Palindrome String conversion:-

                  String pl_str = "MOM";

asked Nov 5, 2015 in Java by Deepa Hotchandani

2 Answers

+1 vote

First of all, i would like to explain about "String Palindrome". A String is said to be Palindrome if it’s value is same when reversed. For Example "MOM" after reverse you will get "MOM". String should be Palindrome.

See Following code to check String Palindrome:-

private static boolean isPlaindrome(String str) {
        StringBuilder sb = new StringBuilder(str);
        String reverseString = sb.reverse().toString();
        if(str.equals(reverseString)){
            return true;
        }
        return false;
    }

answered Nov 7, 2015 by john12 (2,680 points)
0 votes

With the help of following example, i am going to explain about string palindrome. To check string is palindrome or not, I am using string characters position. If first and last character are same and second and last second are same and so on. When all characters are same to this series then we can say that given string is palindrome otherwise not.

String Palindrome Check code:-

private static boolean isPlaindromeUsingAlgo(String str) {
        if(str == null){
            return false;
        }
        for(int i=0; i < str.length() / 2; i++){
            if(str.charAt(i) != str.charAt(str.length() - 1)){
                return false;
            }
        }
        return true;
    }

answered Nov 7, 2015 by me_vinod12 (1,960 points)
...