C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program to print smallest and biggest possible palindrome word in a given string
ExplanationIn 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
SolutionPython
#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
Next Topic#
|