Longest Word
PHP
Medium
5 views
Problem Description
Print the longest word in the line (if tie, print the first).
Input Format
One line string s.
Sample Test Case
Input:
I love programming a lot
Official Solution
<?php
$inputText=rtrim(stream_get_contents(STDIN));
if($inputText==='') exit;
$words=preg_split('/\\s+/', trim($inputText));
$best='';
foreach($words as $w){
if(strlen($w)>strlen($best)) $best=$w;
}
echo $best;
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!