A palindrome string is a string whose reversed string is equal to the original string. In this Java program, we will see how to print the smallest and largest palindrome words in a string. Given a word sentence in the form of a string, we need to print the smallest and longest palindrome word among these 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
Approach:
Example:
// 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
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.