Skip to content

Instantly share code, notes, and snippets.

@Kiwipup
Created September 14, 2018 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kiwipup/354965d0c1f05336b957acc54c2a61d0 to your computer and use it in GitHub Desktop.
Save Kiwipup/354965d0c1f05336b957acc54c2a61d0 to your computer and use it in GitHub Desktop.
PHP letter changes
<?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')));
?>
@MatthewGidcomb
Copy link

Looks good. Clever use of range and in_array to decide whether to change each character.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment