Java Program to find the minimum and maximum palindrome String in a Sentence

 class minMaxPalindrome

{
public static void main(String [] arg)
{

String str ="wow you kaya a kayak";

//convert the string into String Array by using Split function
String[] str1=str.split(" ");

//Initialize the 'length' and 'max' value as 0
int length=0;
int max=0;

//Initialize the 'min' value as the first string of String Array 'str1'
int min=str1[0].length();
int f    //flag variable
int i,j,k;

String wordmaxpalindrome="", minpalindrome="";


for(i=0;i<str1.length;i++)
{   
    f=0;
    //finding the palindrome string from the String Array 'Str1'
    for(j=0,k=str1[i].length()-1;j<k;j++,k--)
        {
           if(str1[i].charAt(j)!=str1[i].charAt(k))
              { f=1;
                break;
               } 

         }
    //Storing the palindrome String in 'word' and its length in 'length'
      if(f==0)
      {
       length=str1[i].length();
        word = str1[i];
      }
      
      //comparing the length of maxmimum palindrome string
      if(max<length)
        { max = length;
          maxp = str1[i];
           }
      
      //comparing the length of minimum palindrome string
      if(min>length)
       { min = length;
         minp = str1[i];
          }

}

      //printing the  maximum and minimum palindrome

    System.out.println("max palindrome word :"+maxp +" "+max);
    System.out.print("min palindrome word :"+minp +" "+min);
}
}

Comments

Popular posts from this blog

Java Program to replace vowel with next immediate character alphabetically in a String.

Java Program to count and output the number of double letter sequences that exist in the string.