rekursive palindrome java code example
Example: 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));
}