Fun at zangOOr Click Here
3 like 0 dislike
140 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 (950 points) | 140 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 (3.3k 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 (640 points)

Related questions

2 like 0 dislike
1 answer
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
asked Aug 2, 2018 in Education & Reference by Sam (1.4k points) | 120 views
1 like 0 dislike
1 answer
asked Jun 17 in Education & Reference by Simmi (640 points) | 69 views
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
1 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,245 questions
1,400 answers
565 comments
4,809 users