Example #1
1
<?php

/*
	Using PHP, have the function LetterChanges(str) take the str parameter being passed and modify it using the following algorithm. 
	Replace every letter in the string with the letter following it in the alphabet (ie. c becomes d, z becomes a).
	Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string. 
*/
function LetterChanges($str)
{
    $returnStr = '';
    $charTemp = '';
    for ($i = 0; $i < strlen($str); $i++) {
        if (preg_match("/^[a-zA-Z]+\$/", $str[$i])) {
            $charTemp = chr(ord($str[$i]) + 1);
            if ($charTemp == 'a' || $charTemp == 'e' || $charTemp == 'i' || $charTemp == 'o' || $charTemp == 'u') {
                $charTemp = strtoupper($charTemp);
            }
            $returnStr = $returnStr . $charTemp;
        } else {
            $returnStr = $returnStr . $str[$i];
        }
    }
    return $returnStr;
}
echo LetterChanges("mama mula ramu");
Example #2
0
<?php

function LetterChanges($str)
{
    $new_string = "";
    // code goes here
    for ($char_location = 0; $char_location <= strlen($str); $char_location++) {
        $char = substr($str, $char_location, 1);
        if (ctype_alpha($char)) {
            $new_char = ++$char;
            if ($new_char == 'a' || $new_char == 'e' || $new_char == 'i' || $new_char == 'o' || $new_char == 'u') {
                $upper_new_char = strtoupper($new_char);
                $new_string .= $upper_new_char;
            } else {
                $new_string .= $new_char;
            }
        } else {
            //don't modify character
            $new_string .= $char;
        }
    }
    return $new_string;
}
// keep this function call here
// to see how to enter arguments in PHP scroll down
echo LetterChanges(fgets(fopen('php://stdin', 'r')));