java check palindrome with string builder code example

Example 1: generate palindrome java

//This function generates a palindrom by recursively reversing a String,
//recursively add it and checks if it's a palindrom

public static String generatePalindrom(String s) {
	return s.equals(reverseString(s)) ? s
				: generatePalindrom(String.valueOf(Integer.parseInt(s) + Integer.parseInt(reverseString(s))));
}

public static String reverseString(String s) {
	return s.length() == 1 ? s : s.charAt(s.length() - 1) + reverseString(s.substring(0, s.length() - 1));
}

Example 2: java check palindrome with string builder

public static boolean isPalindrome(String str){  
    StringBuilder sb=new StringBuilder(str);  
    sb.reverse();  
    String rev=sb.toString();  
    if(str.equals(rev)){  
        return true;  
    }else{  
        return false;  
    }  
}

Tags:

Misc Example