Open In App

Java Program to Print Smallest and Biggest Possible Palindrome Word in a Given String

Improve
Improve
Like Article
Like
Save
Share
Report

A Palindrome String is a string whose reversed string is equal to the original string. In this Java Program, we are going to see the approach of printing the smallest and biggest palindrome word in a String.

Given a sentence of words in the form of a string, and we need to print the smallest and longest palindromic word out of those words.

Example:

Input: Wow madam is driving racecar.

Smallest Palindrome: Wow
Longest Palindrome: racecar
Explanation: The string contains three palindrome words (i.e., madam, Wow, racecar) 
         but the length of racecar is greatest and that of wow is smallest.


Input: "Nitin is a good guy"

Smallest Palindrome: a
Longest Palindrome: Nitin

Approach:

  • lengthPalindrome() function finds the longest and smallest palindrome word by extracting every word of the string and passing it to checkPalin() function and then updating the answer string for the smallest and longest string by comparing it with the length of the original string if it is a palindrome.
     
  • checkPalin() function checks if the word is palindrome. It returns true if word is palindrome else returns false. It makes sure that empty strings are not counted as palindrome as the user may enter more than one spaces in between or at the beginning of the string

Example:

Java




// Java program to print the smallest and
// longest palindromic word out of the words of a sentence
  
public class Main {
  
    // Function to check if a
    // word is palindrome
    public static boolean checkPalin(String word)
    {
  
        int n = word.length();
  
        // making the check case
        // case insensitive
        word = word.toLowerCase();
  
        // loop to check palindrome
        for (int i = 0; i < n; i++, n--)
        {
            if (word.charAt(i) != word.charAt(n - 1))
                return false;
        }
  
        return true;
    }
  
    // Determine the smallest and biggest
    // palindromes in a given string
    public static void lengthPalindrome(int temp, String words[])
    {
        
        int count = 0;
        String smallest = "", longest = "";
        
        for (int i = 0; i < temp; i++) {
            
            if (checkPalin(words[i])) {
                count++;
                
                // Initialize smallest and longest
                // when first palindromic word
                // is found
                if (count == 1)
                    smallest = longest = words[i];
  
                // Compare smallest and longest with each
                // palindromic words
                else {
                    
                    // If length of smallest is greater
                    // than next palindromic word then
                    // Store that word in smallest
                    if (smallest.length()
                        > words[i].length())
                        smallest = words[i];
  
                    // If length of longest is less
                    // than next palindromic word then
                    // Store that word in longest
                    if (longest.length()
                        < words[i].length())
                        longest = words[i];
                }
            }
        }
  
        if (count == 0)
            
            System.out.println("No palindrome found");
        
        else {
            
            System.out.println("Smallest palindrome: "
                               + smallest);
            System.out.println("Biggest palindrome: "
                               + longest);
        }
    }
  
    public static void main(String[] args)
    {
        
        String string = "Wow Madam is driving racecar";
        String word = "";
        
        String[] words = new String[100];
        int temp = 0;
  
        // Add extra space after string
        // to get the last word
        string = string + " ";
  
        for (int i = 0; i < string.length(); i++) {
            
            // Split the string into words
            if (string.charAt(i) != ' ') {
                word = word + string.charAt(i);
            }
            
            else {
                
                // Add word to array words
                words[temp] = word;
                temp++;
                word = "";
            }
        }
        
        System.out.println("Inputted String : " + string);
        lengthPalindrome(temp, words);
    }
}


Output

Inputted String : Wow Madam is driving racecar 
Smallest palindrome: Wow
Biggest palindrome: racecar


Last Updated : 04 Dec, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads