Skip to content

Commit 762c038

Browse files
committed
add decode() to example.php
1 parent 91704f4 commit 762c038

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

exercises/practice/atbash-cipher/.meta/example.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,24 @@ function encode($string)
2626

2727
return implode('', $encoded);
2828
}
29+
30+
function decode($string)
31+
{
32+
$a_z = range('a', 'z');
33+
$z_a = range('z', 'a');
34+
35+
$encodedString = str_replace(' ', '', $string);
36+
37+
$decoded = [];
38+
foreach (str_split($encodedString) as $char) {
39+
// Check if the character is numeric
40+
if ($char >= '0' && $char <= '9') {
41+
$decoded[] = $char;
42+
} elseif ($char >= 'a' && $char <= 'z') {
43+
// Map it from z_a back to a_z
44+
$decoded[] = $a_z[array_search($char, $z_a)];
45+
}
46+
}
47+
48+
return implode('', $decoded);
49+
}

0 commit comments

Comments
 (0)