How to find the largest and the smallest word in a string with PHP

1 Answer

0 votes
$s = "php python c++ c c# java";
 
$words = explode(" ", $s);

$max = "";
$min = $s;
 
foreach ($words as &$w) 
{
    if (strlen($w) > strlen($max)) $max = $w;  
    if (strlen($w) < strlen($min)) $min = $w;  
}   

echo "The largest word is: " . $max . "<br />";
echo "The smallest word is: " . $min . "<br />";


/*
run:
   
The largest word is: python
The smallest word is: c
    
*/

 



answered Mar 23, 2017 by avibootz

Related questions

...