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.
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
- 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
Inputted String : Wow Madam is driving racecar Smallest palindrome: Wow Biggest palindrome: racecar
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.