# Encode the alphabet cipher

Given a string that contains only lowercase letters, encode that string with the alphabet cipher.

To encode with the alphabet cipher (I will be using the example hello):

1. First, convert each letter in the string to a number depending on its position in the alphabet (a = 1, b = 2, etc.) Example: 8 5 12 12 15
2. Pad each number to two characters with 0s. Example: 08 05 12 12 15
3. Join. Example: 0805121215

## Test cases

helloworld -> 08051212152315181204
codegolf -> 0315040507151206
alphabetcipher -> 0112160801020520030916080518
johncena -> 1015081403051401


Remember, this is , so the code with the fewest number of bytes wins.

## GolfSharp,(non competing) 37 bytes

s=>s.s(n=>(n<106?"0":"")+(n-96)).j();


# Swift 3, 51

Dependency: Foundation for String(format:_:)

{$0.utf8.map{String(format:"%02d",$0-96)}.joined()} // where $0 is String ## Usage: Test "codegolf".utf8.map{String(format:"%02d",$0-96)}.joined()

or

{$0.utf8.map{String(format:"%02d",$0-96)}.joined()}("codegolf")

# MATLAB / Octave, 32 24 bytes

@(s)sprintf('%02d',s-96)


## Explanation

1. @(s) denotes an anonymous function whose input is expected to be a string and stored in the variable s.
2. The ASCII code for the letter a is 97. Therefore, subtracting the input string by 96 coalesces the string so that it becomes an array transforming the string into an array of numbers enumerated from 1 to 26, so we're now at input('','s')-96.
3. Using sprintf with the formatting specifier %02d takes the numbers in the array and ensures that there are 2 digits to output for each number. The numbers are thus combined to a single string and we output a single string. We will also pad the first digit with a 0 in case there is only 1 digit in an array.

## Examples

>> f=@(s)sprintf('%02d',s-96)
>> f('helloworld')

ans =

08051212152315181204

>> f('codegolf')

ans =

0315040507151206

>> f('alphabetcipher')

ans =

0112160801020520030916080518

>> f('johncena')

ans =

1015081403051401


# Powershell, 39 bytes

-join($args|% t*y|%{'{0:D2}'-f($_-96)})


Test script:

$f = { -join($args|% t*y|%{'{0:D2}'-f($_-96)}) } @( ,("helloworld" , "08051212152315181204") ,("codegolf" , "0315040507151206") ,("alphabetcipher" , "0112160801020520030916080518") ,("johncena" , "1015081403051401") ) | % {$s,$expected =$_
$result = &$f $s "$($result-eq$expected): $result" }  Output: True: 08051212152315181204 True: 0315040507151206 True: 0112160801020520030916080518 True: 1015081403051401  ## Powershell, 42 bytes -join($args|% t*y|%{'0'*($_-le105);$_-96})


or

-join($args|% t*y|%{if($_-le105){0}$_-96})  # Dart, 58 bytes f(s)=>s.runes.map((t)=>'${t-96}'.padLeft(2,'0')).join('');


Try it online!

Other 58 bytes solution :

f(s)=>s.runes.map((t)=>(t>106?'0':'')+'${t-96}').join('');  Try it online! # C# (Visual C# Interactive Compiler), 43 bytes n=>string.Concat(n.Select(c=>$"{c%32:D2}"))


Try it online!

# Alternative taking in a List<char>, 36 bytes

n=>n.ForEach(c=>Write($"{c%32:D2}"))  Try it online! • List<char> is a valid way to take in a string – ASCII-only Feb 24 '19 at 21:27 • I know, I just thought of the alternative after my first solution so I put it after it – Embodiment of Ignorance Feb 24 '19 at 21:30 # Rust, 197 191 181 bytes |t:&String|->Option<String>{let a="abcdefghijklmnopqrstuvwxyz";let mut o=String::new();for c in t.chars(){let n=a.find(c)?+1;if n<10{o.push('0');}o.push_str(&n.to_string());}Some(o)  Reduced number of bytes thanks to Jonathan Fretch. Code should be runnable at https://repl.it/repls/RightMiniFunctions Reduced number of bytes again thanks to ASCII-only. • Can you not save some bytes by removing whitespace in n < 10 or n = a.find(c)? Furthermore, would you mind adding a link to an online testing environment for ease of verification? – Jonathan Frech Feb 24 '19 at 17:05 • You should at least rename the function to a 1-character name. Also, lambdas (anonymous functions) are valid – ASCII-only Feb 24 '19 at 21:28 # Python 3, 47 bytes f=lambda x:"".join(f"{ord(j)-96:02}"for j in x)  Try it online! # Japt -m, 8 bytes c +4 s Å  Run it online ## MBASIC, 112 bytes I could save 8 bytes on line 2 if STR$() didn't insist on including a leading space.

1 INPUT S$:FOR I=1 TO LEN(S$):A=ASC(MID$(S$,I,1))-96:IF A<10 THEN O$=O$+"0"
2 O$=O$+MID$(STR$(A),2):NEXT:PRINT O\$


Sample output

? alphabetcipher
0112160801020520030916080518


# Python 2, 514945 42 bytes

lambda x:"".join(ord(i)+4[1:]for i in x)


Try it online!

Golfed to 49 bytes because .join will accept generators too

Golfed to 45 bytes because of lambdas

Golfed to 42 bytes because of switching to Python 2 and using  (repr)

EXPLANATION:

Uses the trick in the 05AB1E answer.

lambda x:                               Declare a lambda accepting the string
"".join(                            Join by the empty string
                              Repr (string representation)...
ord(                       Unicode codepoint (A -> 65, a -> 97)
i                      The iterator in the for loop
)

[1:]                           With the first character removed
) for i in x                        While a char i is in the string x


# Stax, 5 bytes

öÇIªÆ


Run and debug it

For each character

• add 4 to the codepoint e.g. 108
• convert to string e.g. "108"
• drop the first character e.g. "08"

# GolfScript, 10 bytes

In GolfScript converting to a character is unneccecary because strings are character arrays.

{4+''+1>}%


Try it online!

## Explanation

{       }% # Foreach over the implicit codepoint list
4+        # Add 4 to every item of the list
''+     # Convert to a string
1>   # Filter out all characters after the 1st character,
# Including the 1st character