Reputation: 43
I am working through the Coderbyte challenges as I am teaching myself some PHP.
I have been working through the below challenge (link below)
I have got the to below and I a bit confused as it now capitalises each letter of the array rather than the letters selected in the 'if statement'.
I am eager to learn and dont just simply want an answer without an explanation. If you could tell me where I am going wrong and if I am doing things in a long winded approach.
Thanks for your help.
<?php
function LetterChanges($str) {
// code goes here
$str = strtolower($str);
$strArray = str_split($str);
for($i = 0; $i < strlen($str); $i++){
++$strArray[$i];
if($strArray[$i] == "aa"){
$strArray[$i] = "A";
}
elseif($strArray[$i] == "e" || "i" || "o" || "u"){
$strArray[$i] = strtoupper($strArray[$i]);
}
}
return implode ($strArray);
}
// keep this function call here
// to see how to enter arguments in PHP scroll down
echo LetterChanges(fgets(fopen('php://stdin', 'r')));
?>
Upvotes: 3
Views: 90
Reputation: 2071
You can use this code
function LetterChanges($str){
$arr = array();
$strlen = strlen( $str );
for( $i = 0; $i <= $strlen; $i++ ) {
$char = substr( $str, $i, 1 );
++$char;
if($char == "a" || $char == "e" || $char== "i" || $char== "o" || $char== "u"){
$char = strtoupper($char);
}
if($char == "aa"){
$char = 'A'; //When we increase Z it becomes aa so we changed it to A
}
$arr[] = $char;
}
//print_r($arr);
echo implode("",$arr);
}
LetterChanges('hello*3');
Explanation
In for
loop get each character separately in an array then increase it by one then in that increased character we check for vowels if they present then we change them uppercase and again change that array to simple string.
Upvotes: 2
Reputation: 473
Try this.. It will work
<?php
function LetterChanges($str) {
$strlen = strlen( $str );
$out = '';
for($i = 0; $i <= $strlen; $i++) {
$char = substr($str,$i,1);
if(!is_numeric($char)) {
$nextChar = ++$char;
if (strlen($nextChar) > 1) { // if you go beyond z or Z reset to a or A
$nextChar = $next_ch[0];
}
if(in_array($char,array('a','e','i','o','u'))) {
$nextChar = strtoupper($nextChar);
}
}
else {
$nextChar = $char;
}
$out .= $nextChar;
}
$str = $out;
// code goes here
return $str;
}
// keep this function call here
// to see how to enter arguments in PHP scroll down
echo LetterChanges(fgets(fopen('php://stdin', 'r')));
?>
Upvotes: 0
Reputation:
You cannot increment a character using like this => "++$strArray[$i];" For this you have to first get the ascii value of that character in php you could get ascii value of character using ord() function then increment that ascii value by one and then conver it back into character using chr() function.
Here is full solution to your problem:
function LetterChanges($str) {
// code goes here
$str = strtolower($str);
$strarray = str_split($str);
$newarray = array();
for($i=0; $i<count($strarray); $i++) {
if(ord($strarray[$i])>=97 && ord($strarray)<=122) {
$newarray[$i] = chr(ord($strarray[$i])+1);
} else {
$newarray[$i] = $strarray[$i];
}
if($newarray[$i]=='a' || $newarray[$i]=='e' || $newarray[$i]=='i' || $newarray[$i]=='o' || $newarray[$i]=='u') {
$newarray[$i] = strtoupper($newarray[$i]);
}
}
$str = implode('',$newarray);
return $str;
}
Upvotes: 0