Created
November 30, 2018 16:13
-
-
Save ammardev/16521b747a678eb9e0665a905f13b22f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# Problem 2: | |
# Function: | |
function listFramer(array $list): string { | |
$longestWord = 0; | |
foreach($list as $item) | |
if(($itemLength = strlen($item)) > $longestWord) | |
$longestWord = $itemLength; | |
$result = str_repeat('*', $longestWord + 4). PHP_EOL; | |
foreach($list as $item) | |
$result .= '* '. $item . str_repeat(' ', $longestWord-strlen($item)) . " *\n"; | |
$result .= str_repeat('*', $longestWord + 4) . PHP_EOL; | |
return $result; | |
} | |
# Function Usage: | |
echo 'Insert a list of strings: '; | |
$stringList = fgets(fopen("php://stdin","r")); | |
$list = json_decode($stringList); | |
echo listFramer($list); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment