Longest Word

Longest Word

Medium PHP PHP Strings 36 views
Explanation Complexity

Problem Statement

Print the longest word in the line (if tie, print the first).

Input Format

One line string s.

Output Format

One word.

Example

I love programming a lot
programming

Constraints

Total chars

Input / Output Format

Input Format
One line string s.
Output Format
One word.
Constraints
Total chars

Examples

Input:
I love programming a lot
Output:
programming

Example Solution (Public)

PHP
<?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;
?>

Official Solution Code

<?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;
?>
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.