Skip to content

Commit

Permalink
add decode() to example.php
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasnorre committed Nov 11, 2024
1 parent 91704f4 commit 762c038
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions exercises/practice/atbash-cipher/.meta/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,24 @@ function encode($string)

return implode('', $encoded);
}

function decode($string)
{
$a_z = range('a', 'z');
$z_a = range('z', 'a');

$encodedString = str_replace(' ', '', $string);

$decoded = [];
foreach (str_split($encodedString) as $char) {
// Check if the character is numeric
if ($char >= '0' && $char <= '9') {
$decoded[] = $char;
} elseif ($char >= 'a' && $char <= 'z') {
// Map it from z_a back to a_z
$decoded[] = $a_z[array_search($char, $z_a)];
}
}

return implode('', $decoded);
}

0 comments on commit 762c038

Please sign in to comment.