Explanation
In this program, we need to find the smallest and biggest palindromic word present in the given string.
Wow you own kayak
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
- Define a string.
- Convert the string into lowercase to make it case-insensitive.
- Add an extra space at the end.
- 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.
- 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.
- 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.
- When first palindrome is found then, set smallPalin and bigPalin with first palindrome.
- When next palindrome is found, check if the length of that palindrome is less than smallPalin. If yes, store that word in smallPalin.
- If length of next palindrome is greater than bigPalin. If yes, store that word in bigPalin.
- At the end, if count is equal to 0, then there is no palindrome. Else, display the smallest and biggest palindrome.
Input:
string = "Wow you own kayak"
Output:
Smallest palindromic word: wow
Biggest palindromic word: kayak
Python
Output:
C
Output:
JAVA
Output:
C#
Output:
PHP
Output: