A String can contain palindrome strings in it and to find longest palindrome in given String is a programming question.
Check this post for complete program to find longest palindrome in a String.
public class PalinDemo {
public static void main(String[] args) {
PalinDemo pd = new PalinDemo();
String Palin = pd.findLongestPalinindrome("bananas");
System.out.println("" + Palin);
Palin = pd.findLongestPalinindrome("abaradar121");
System.out.println("" + Palin);
}
public String findLongestPalinindrome(String s) {
// Validations
if (s.isEmpty()) {
return "Please enter a String";
}
if (s.length() == 1) {
return s;
}
// Validations end
// Start with one char (starting) as a longest Palinindrome
String longest = s.substring(0, 1);
for (int i = 0; i < s.length(); i = i+1) {
// get longest Palinindrome for odd length (center is i)
String tmp = checkForEquality(s, i, i);
if (tmp.length() > longest.length()) {
longest = tmp;
}
// get longest Palinindrome for even length (center is i, i+1)
tmp = checkForEquality(s, i, i + 1);
if (tmp.length() > longest.length()) {
longest = tmp;
}
}
return longest;
}
/**
* In this method equality is checked starting from
* the center moving one character left and one character
* right from the center. If both chars are equal then the
* next set of chars are checked.
*
*/
public String checkForEquality(String s, int begin, int end) {
while (begin >= 0 && end <= s.length() - 1 && s.charAt(begin) == s.charAt(end)) {
begin--;
end++;
}
return s.substring(begin + 1, end);
}
}