palindromes in java code example
Example 1: generate palindrome java
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: is palindrome method in java
package test
public boolean isPalindromString(String text){
String reverse = reverse(text);
if(text.equals(reverse))
{
return true;
}
return false;
}
public String reverse(String input)
{
if(input == null || input.isEmpty())
{
return input;
}
return input.charAt(input.length()- 1) + reverse
(input.substring(0, input.length() - 1));
}