Write a program in java to find the longest palindrome from the given String.
If the length is same then print in descending order.
Input : madamLeveLrotornitin
Total Palindromes = madam ada LeveL eve rotor oto nitin iti
Longest Palindrome : rotor
class Palindrome4
{
public static void main(String[] args) {
// String s="madamLeveLnitin";
// String s="madamnitin";
String s="madamLeveLrotornitin";
String s2="";String s3="";
System.out.print("Total Palindromes = ");
for(int i=0;i<s.length();i++)
{
for(int j=i+2;j<=s.length();j++)
{
s2=s.substring(i,j);
StringBuffer sb=new StringBuffer(s2).reverse();
if(s2.equals(sb.toString()))
{
System.out.print(s2+" ");
if(s2.length()>s3.length())
s3=s2;
else if(s2.length()==s3.length())
{
if(s3.compareTo(s2)<0)
s3=s2;
}
}
}
}
System.out.println("\n\nLongest Palindrome = "+s3);
}
}
Write a program in java to find the longest palindrome from the given String. [All Java Interview Programs] |
0 Comment to "Write a program in java to find the longest palindrome from the given String. If the length is same then print in descending order."
Post a Comment