Q:

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

belongs to collection: String Programs

0

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

  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.

Input:

string = "Wow you own kayak"  

Output:

Smallest palindromic word: wow
Biggest palindromic word: kayak

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python

#isPalindrome() checks whether a string is palindrome or not  
def isPalindrome(a):  
    flag = True;  
    #Iterate the string forward and backward and compare one character at a time  
    #till middle of the string is reached  
    for i in range(0, len(a)//2):  
        if(a[i] != a[len(a) -i-1]):  
            flag = False;  
            break;  
    return flag;  
      
string = "Wow you own kayak";  
words = [];  
word = "";  
count = 0;  
   
#Converts the given string into lowercase  
string = string.lower();  
   
#Add extra space after string to get the last word in the given string  
string = string + " ";  
   
for i in range(0, len(string)):  
    #Split the string into words  
    if(string[i] != ' '):  
        word = word + string[i];  
    else:  
        #Add word to array words  
        words.append(word);  
        #Make word an empty string  
        word = "";  
          
#Determine the smallest and biggest palindromes in a given string  
for i in range(0, len(words)):  
    if(isPalindrome(words[i])):  
          
        count = count + 1;  
        #When first palindromic word is found  
        if(count == 1):  
            #Initialize smallPalin and bigPalin with first palindromic word  
            smallPalin = bigPalin = words[i];  
              
        #Compare smallPalin and bigPalin with each palindromic words  
        else:  
            #If length of smallPalin is greater than next palindromic word  
            #Store that word in smallPalin  
            if(len(smallPalin) > len(words[i])):  
                smallPalin = words[i];  
                  
            #If length of bigPalin is less than next palindromic word  
            #Store that word in bigPalin  
            if(len(bigPalin) < len(words[i])):  
                bigPalin = words[i];  
   
if(count == 0):  
    print("No palindrome is present in the given string");  
else:  
    print("Smallest palindromic word: " + smallPalin);  
    print("Biggest palindromic word: " + bigPalin);  

 

Output:

Smallest palindromic word: wow
Biggest palindromic word: kayak

 

C

#include <stdio.h>  
#include <stdbool.h>  
#include <string.h>  
   
//isPalindrome() checks whether a string is palindrome or not  
bool isPalindrome(char a[]){  
    bool flag = true;  
    //Iterate the string forward and backward and compare one character at a time   
    //till middle of the string is reached  
    for(int i = 0; i < strlen(a)/2; i++){  
        if(a[i] != a[strlen(a)-i-1]){  
            flag = false;  
            break;  
        }  
    }  
    return flag;  
}  
   
int main()  
{     
    char string[] = "Wow you own kayak";  
    char words[100][100], smallPalin[100], bigPalin[100];  
    int i = 0, j = 0, k, length, count = 0;  
      
    //Split the string into words such that each row of array words represents a word  
    for(k=0; string[k]!='\0'; k++){  
        //Here, i represents row and j represents column of two-dimensional array words   
        if(string[k] != ' ' && string[k] != '\0'){  
            words[i][j++] = tolower(string[k]);  
        }  
        else{  
            words[i][j] = '\0';  
            //Increment row count to store new word  
            i++;  
            //Set column count to 0  
            j = 0;  
        }  
    }  
    //Store row count in variable length  
    length = i+1;  
      
    //Determine the smallest and biggest palindromes in a given string  
    for(int i = 0; i < length; i++){  
        if(isPalindrome(words[i])){  
              
            count++;  
            //When first palindromic word is found  
            if(count == 1){  
                //Initialize smallPalin and bigPalin with first palindromic word  
                strcpy(smallPalin, words[i]);  
                strcpy(bigPalin, words[i]);  
            }  
              
            //Compare smallPalin and bigPalin with each palindromic words  
            else{  
                //If length of smallPalin is greater than next palindromic word   
                //Store that word in smallPalin  
                if(strlen(smallPalin) > strlen(words[i]))  
                    strcpy(smallPalin, words[i]);  
                      
                //If length of bigPalin is less than next palindromic word   
                //Store that word in bigPalin  
                if(strlen(bigPalin) < strlen(words[i]))  
                    strcpy(bigPalin, words[i]);  
            }  
        }  
    }  
      
    if(count == 0)  
        printf("No palindrome is present in the given string");  
    else{  
        printf("Smallest palindromic word: %s\n", smallPalin);  
        printf("Biggest palindromic word: %s", bigPalin);  
    }  
      
    return 0;  
}  

 

Output:

Smallest palindromic word: wow
Biggest palindromic word: kayak

 

JAVA

public class SmallestBiggestPalindrome  
{  
    //isPalindrome() checks whether a string is palindrome or not  
    public static boolean isPalindrome(String a){  
        boolean flag = true;  
        //Iterate the string forward and backward and compare one character at a time   
        //till middle of the string is reached  
        for(int i = 0; i < a.length()/2; i++){  
            if(a.charAt(i) != a.charAt(a.length()-i-1)){  
                flag = false;  
                break;  
            }  
        }  
        return flag;  
    }  
      
    public static void main(String[] args){  
        String string = "Wow you own kayak";  
        String word = "", smallPalin = "", bigPalin="";  
        String[] words = new String[100];  
        int temp = 0, count = 0;  
          
        //Converts the given string into lowercase  
        string = string.toLowerCase();  
          
        //Add extra space after string to get the last word in the given string  
        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;  
                //Increment temp  
                temp++;  
                //Make word an empty string  
                word = "";  
            }  
        }  
          
        //Determine the smallest and biggest palindromes in a given string  
        for(int i = 0; i< temp; i++){  
            if(isPalindrome(words[i])){  
                  
                count++;  
                //When first palindromic word is found  
                if(count == 1)  
                    //Initialize smallPalin and bigPalin with first palindromic word  
                    smallPalin = bigPalin = words[i];  
                  
                //Compare smallPalin and bigPalin with each palindromic words  
                else{  
                    //If length of smallPalin is greater than next palindromic word   
                    //Store that word in smallPalin  
                    if(smallPalin.length() > words[i].length())  
                        smallPalin = words[i];  
                          
                    //If length of bigPalin is less than next palindromic word   
                    //Store that word in bigPalin  
                    if(bigPalin.length() < words[i].length())  
                        bigPalin = words[i];  
                }  
            }  
        }  
          
        if(count == 0)  
            System.out.println("No palindrome is present in the given string");  
        else{  
            System.out.println("Smallest palindromic word: " + smallPalin);  
            System.out.println("Biggest palindromic word: " + bigPalin);  
        }  
    }  
}  

 

Output:

Smallest palindromic word: wow
Biggest palindromic word: kayak

 

C#

using System;  
                      
public class SmallestBiggestPalindrome  
{      
    //isPalindrome() checks whether a string is palindrome or not  
    public static Boolean isPalindrome(String a){  
        Boolean flag = true;  
        //Iterate the string forward and backward and compare one character at a time   
        //till middle of the string is reached  
        for(int i = 0; i < a.Length/2; i++){  
            if(a[i] != a[a.Length-i-1]){  
                flag = false;  
                break;  
            }  
        }  
        return flag;  
    }  
      
    public static void Main()  
    {  
        String string1 = "Wow you own kayak";  
        String word = "", smallPalin = "", bigPalin="";  
        String[] words = new String[100];  
        int temp = 0, count = 0;  
          
        //Converts the given string into lowercase  
        string1 = string1.ToLower();  
          
        //Add extra space after string1 to get the last word in the given string  
        string1 = string1 + " ";  
          
        for(int i = 0; i < string1.Length; i++){  
            //Split the string into words  
            if(string1[i] != ' '){  
                word = word + string1[i];  
            }  
            else{  
                //Add word to array words  
                words[temp] = word;  
                //Increment temp  
                temp++;  
                //Make word an empty string  
                word = "";  
            }  
        }  
          
        //Determine the smallest and biggest palindromes in a given string  
        for(int i = 0; i< temp; i++){  
            if(isPalindrome(words[i])){  
                  
                count++;  
                //When first palindromic word is found  
                if(count == 1)  
                    //Initialize smallPalin and bigPalin with first palindromic word  
                    smallPalin = bigPalin = words[i];  
                  
                //Compare smallPalin and bigPalin with each palindromic words  
                else{  
                    //If length of smallPalin is greater than next palindromic word   
                    //Store that word in smallPalin  
                    if(smallPalin.Length > words[i].Length)  
                        smallPalin = words[i];  
                          
                    //If length of bigPalin is less than next palindromic word   
                    //Store that word in bigPalin  
                    if(bigPalin.Length < words[i].Length)  
                        bigPalin = words[i];  
                }  
            }  
        }  
          
        if(count == 0)  
            Console.WriteLine("No palindrome is present in the given string");  
        else{  
            Console.WriteLine("Smallest palindromic word: " + smallPalin);  
            Console.WriteLine("Biggest palindromic word: " + bigPalin);  
        }  
    }  
}  

 

Output:

Smallest palindromic word: wow
Biggest palindromic word: kayak

 

PHP

<!DOCTYPE html>  
<html>  
<body>  
<?php  
//isPalindrome() checks whether a string is palindrome or not  
function isPalindrome($a){  
    $flag = true;  
    //Iterate the string forward and backward and compare one character at a time   
    //till middle of the string is reached  
    for($i = 0; $i < strlen($a)/2; $i++){  
        if($a[$i] != $a[strlen($a)-$i-1]){  
            $flag = false;  
            break;  
        }  
    }  
    return $flag;  
}  
   
$string = "Wow you own kayak";  
$word = "";  
$words = array();  
$count = 0;  
   
//Converts the given string into lowercase  
$string = strtolower($string);  
   
//Add extra space after string to get the last word in the given string  
$string = $string . " ";  
   
for($i = 0; $i < strlen($string); $i++){  
    //Split the string into words  
    if($string[$i] != ' '){  
        $word = $word . $string[$i];  
    }  
    else{  
        //Add word to array words  
        array_push($words, $word);  
        //Make word an empty string  
        $word = "";  
    }  
}  
   
//Determine the smallest and biggest palindromes in a given string  
for($i = 0; $i< count($words); $i++){  
    if(isPalindrome($words[$i])){  
          
        $count++;  
        //When first palindromic word is found  
        if($count == 1)  
            //Initialize smallPalin and bigPalin with first palindromic word  
            $smallPalin = $bigPalin = $words[$i];  
          
        //Compare smallPalin and bigPalin with each palindromic words  
        else{  
            //If length of smallPalin is greater than next palindromic word   
     //Store that word in smallPalin  
     if(strlen($smallPalin) > strlen($words[$i]))  
         $smallPalin = $words[$i];  
           
     //If length of bigPalin is less than next palindromic word   
     //Store that word in bigPalin  
     if(strlen($bigPalin) < strlen($words[$i]))  
         $bigPalin = $words[$i];  
    }  
}  
}  
   
if($count == 0)  
    print("No palindrome is present in the given string");  
else{  
    print("Smallest palindromic word: " . $smallPalin);  
    print("<br>Biggest palindromic word: " . $bigPalin);  
}  
?>  
</body>  
</html>  

 

Output:

Smallest palindromic word: wow
Biggest palindromic word: kayak

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


Program to separate the individual characters from... >>
<< Program to find the number of words in the given t...