PHP Programs

Please click on 1-2 Ads to keep this website's Study Material FREE and regularly Updated.

Program 49: Write a program to print smallest and biggest possible palindrome word in a given string.

smallest_biggest.php

<!DOCTYPE html>  
<html>  
<head>	
	<title>PHP Programs TutorialDuniya.com</title>
</head>

<body>
	<?php
		function is_Palindrome($a)
		{  
			$flag = true;   
			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;  
		
		$string = strtolower($string);  

		$string = $string . " ";  
		
		for($i = 0; $i < strlen($string); $i++)
		{  
			if($string[$i] != ' ')
			{  
				$word = $word . $string[$i];  
			}  
			else
			{  
				array_push($words, $word);
				$word = "";  
			}  
		}  

		for($i = 0; $i< count($words); $i++)
		{  
			if(is_Palindrome($words[$i]))
			{
				$count++;
				if($count == 1)			
					$smallPalindrome = $bigPalindrome = $words[$i];
				else
				{  
					if(strlen($smallPalindrome) > strlen($words[$i]))  
						$smallPalindrome = $words[$i];  

					if(strlen($bigPalindrome) < strlen($words[$i]))  
						$bigPalindrome = $words[$i];  
				}  
			}  
		}  
		
		if($count == 0)  
			echo "No palindrome is present in the given string";  
		else
		{  
			echo "Smallest palindrome word : " . $smallPalindrome; 

			echo "<br>Biggest palindrom word  : " . $bigPalindrome;  
		}  
	?>  
</body>  
</html>



If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.