Claim your Biolink Click Here
3 like 0 dislike
2.6k views
Write a function in which a letter and a number is passed and return the letter incremented by that count and starts with 'a' again after reaching 'z'

// Example

// enc('a', 0) => 'a'
// enc('d', 15) => 's'
// enc('z', 1) => 'a'
// enc('z', 28) => 'b'
in Education & Reference by (1.1k points) | 2.6k views

2 Answers

1 like 0 dislike
Best answer
function autoI($str, $num){
$array = range('a','z');
$arrs = array_search($str,$array);
$val = fmod($arrs+$num+1,26);
return $array[$val-1];
}
echo autoI("z", "28");
by (4.7k points)
0 like 0 dislike

First way

function enc($alphabet, $shift){
  $array = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
  $key = array_search($alphabet, $array);
  $rkey = fmod($key+1+$shift, 26);
  return $array[$rkey-1];
}

echo enc('a',0);

Second Way

function alpha($char,$num)
{
$i = 0;
foreach(range('A','Z') as $elements)
{
$alphabet[$i] = $elements;
$i++;
}
if(array_key_exists($num,$alphabet))
{
$char_index = array_search($char,$alphabet);
$index = $num + $char_index;
if($index >= 26)
{
$index = $index - 26;
$output = $alphabet[$index];
}
else
{
$output = $alphabet[$index];
}
}
else
{
$output =  "out of range";
}

return $output;
}
echo alpha("Z",3);
by (820 points)

Related questions

2 like 0 dislike
2 answers
2 like 0 dislike
1 answer
0 like 0 dislike
2 answers
1 like 0 dislike
1 answer
asked Jun 17, 2021 in Education & Reference by Simmi (820 points) | 286 views
0 like 0 dislike
1 answer
asked Aug 2, 2018 in Education & Reference by Sam (1.6k points) | 253 views
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
1 like 0 dislike
1 answer

Where your donation goes
Technology: We will utilize your donation for development, server maintenance and bandwidth management, etc for our site.

Employee and Projects: We have only 15 employees. They are involved in a wide sort of project works. Your valuable donation will definitely boost their work efficiency.

How can I earn points?
Awarded a Best Answer 10 points
Answer questions 10 points
Asking Question -20 points

1,310 questions
1,471 answers
569 comments
4,809 users