Javatpoint Logo
Javatpoint Logo

Program to print smallest and biggest possible palindrome word in a given string

Explanation

In this program, we need to find the smallest and biggest palindromic word present in the given string.

In above example, wow represent the smallest palindrome and kayak represent the biggest palindrome. One of the approach to accomplish this task is split the string into word. Then, check whether the words are palindrome or not. Then, compare their length to find out the minimum and maximum palindromic word.

Algorithm

  1. Define a string.
  2. Convert the string into lowercase to make it case-insensitive.
  3. Add an extra space at the end.
  4. Now, iterate through the string till space is found and add those characters into variable word. Add each word into words array. Array words will hold all the words present in the string.
  5. isPalindrome() will iterate the string forward and backward and compare a character at a time. If there is no match, then set the flag to false. If flag is true, then string is a palindrome. If flag is false, then string is not a palindrome.
  6. Iterate through array words, and check whether a word is palindrome by calling isPalindrome(). If a word is palindrome, then increment the count by 1. Variable count is used to check whether there are palindromic words present in the string or not.
  7. When first palindrome is found then, set smallPalin and bigPalin with first palindrome.
  8. When next palindrome is found, check if the length of that palindrome is less than smallPalin. If yes, store that word in smallPalin.
  9. If length of next palindrome is greater than bigPalin. If yes, store that word in bigPalin.
  10. At the end, if count is equal to 0, then there is no palindrome. Else, display the smallest and biggest palindrome.

Solution

Python

Output:

Smallest palindromic word: wow
Biggest palindromic word: kayak

C

Output:

Smallest palindromic word: wow
Biggest palindromic word: kayak

JAVA

Output:

Smallest palindromic word: wow
Biggest palindromic word: kayak

C#

Output:

Smallest palindromic word: wow
Biggest palindromic word: kayak

PHP

Output:

Smallest palindromic word: wow
Biggest palindromic word: kayak

Next Topic#




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


B.Tech / MCA