Created
September 14, 2018 02:45
-
-
Save Kiwipup/354965d0c1f05336b957acc54c2a61d0 to your computer and use it in GitHub Desktop.
PHP letter changes
This file contains 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 | |
function LetterChanges($str) { | |
// code goes here | |
$vowels = array('a','e','i','o','u'); | |
$letters = range('a','z'); | |
/* the for loop will continue its iterations | |
as long as $i is less than the number of letters in $str */ | |
for ($i=0; $i<strlen($str); $i++) { | |
/*if the value of $str is found in $letters | |
then the character order of its value will increase by one */ | |
if (in_array($str[$i], $letters)) { | |
$str[$i] = $str[$i] == 'z' ? 'a' : chr( ord($str[$i])+1 ) ; | |
/* if the value of $str can be found in $vowels, | |
then the value of $str will be capitalized */ | |
if(in_array($str[$i], $vowels)){ | |
$str[$i] = strtoupper($str[$i]); | |
} | |
} | |
} | |
/* when the entire length of $str has been iterated through, | |
it's final value is returned and the for loop terminates */ | |
return $str; | |
} | |
// keep this function call here | |
echo LetterChanges(fgets(fopen('php://stdin', 'r'))); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks good. Clever use of
range
andin_array
to decide whether to change each character.