# 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.

• Related. Oct 28, 2016 at 17:09

# Labyrinth, 40 bytes

      ,")@
!{_10%! (
/       _
01_}:-69"


# R, 71 51 bytes

Saved 20 bytes thanks to Billywob. Takes input from stdin and outputs to stdout.

cat(sprintf("%02d",utf8ToInt(scan(,""))-96),sep="")


Examples:

helloworld -> 08051212152315181204

codegolf -> 0315040507151206

alphabetcipher -> 0112160801020520030916080518

johncena -> 1015081403051401

• You can use utf8toInt(scan(,"))-96 instead of the whole match thing. Don't think there's a better way to handle the padding though. Oct 29, 2016 at 10:26
• @Billywob Thanks! For the padding, I tried using formatC earlier but that worked out as needing one more byte than the current approach. Oct 29, 2016 at 14:27

# Actually, 10 bytes

Using the neat algorithm in Adnan's 05AB1E answer. Golfing suggestions welcome. Try it online!

O4+$pXMΣ  Ungolfing  Implicit input s. O ord() every char in s. 4+ Add 4 to every ord in s. ...M Map the following function over s. Variable m.$        Push str(m).
pX       Discard the first char of str(m).
Invariably this is a 1 and we get our ciphered m.
Σ        sum() everything to get one string.
Implicit return.


# Python 3, 47 bytes

f=lambda x:"".join(f"{ord(j)-96:02}"for j in x)


Try it online!

# 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


# Whitespace, 84 bytes

[N
S S N
_Create_Label_LOOP][S S S N
_Push_0][S N
S _Dupe_0][T    N
T   S _Read_STDIN_as_char][T    T   T   _Retrieve_input][S S S T    T   S S S S S N
_Push_96][T S S T   _Subtract][S N
S _Dupe][S S S T    S T S N
_Push_10][T S S T   _Subtract][N
T   T   S N
_If_neg_jump_to_Label_PRINT_0][N
S S T   N
_Create_Label_DONE_WITH_PRINT_0][T  N
S T _Print_as_integer][N
S N
N
_Jump_to_Label_LOOP][N
S S S N
_Create_Label_PRINT_0][S S S N
_Push_0][T  N
S T _Print_as_integer][N
S N
T   N
_Jump_to_Label_DONE_WITH_PRINT_0]


Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs and new-lines only).

Explanation in pseudo-code:

Start LOOP:
Integer c = STDIN as character
c = c - 96
If(c < 10):
Print 0 as integer to STDOUT
Print c as integer to STDOUT
Go to next iteration of LOOP


# K (oK), 13 bytes

Solution:

,//$3 10\'96!  Try it online! Explanation: ,//$3 10\'96! / the solution
96! / input modulo 96
3 10\'    / convert to base 30ish
v o0'v$:a.< v < >/ \K o  Try it online! # Flattened wi.hn$@'-.a:$v'0o>/o\K Full solution > ^ (additional redirections) wi.hn$@               K  Where there are characters to read on the input
'-               Get the letter position in the alphabet
.a:$v'0o If it is less than 10, print a leading 0 o Then print the number  # K (ngn/k), 31 29 bytes ,/{$[1=#$x;"0",$x;$x]}'-96+0+  Try it online! Another function-less answer. Explanation: ,/{$[1=#$x;"0",$x;$x]}'-96+0+ Main function. 0+ Convert each character to ASCII value. -96+ Add with -96 for each of them ' For each of the numbers { } Execute a function that$[               ]               If
$x The number converted to string # Length 1= Is equal to 1 ;"0",$x                   Then, pad "0" before the number

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  ## Try it online! # 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 Feb 24, 2019 at 21:27 • I know, I just thought of the alternative after my first solution so I put it after it Feb 24, 2019 at 21:30 • 32 bytes with SelectMany (but returns an IEnumerable<char>). Apr 24, 2020 at 14:52 # 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? Feb 24, 2019 at 17:05 • You should at least rename the function to a 1-character name. Also, lambdas (anonymous functions) are valid Feb 24, 2019 at 21:28 # 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  # 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  # Erlang (escript), 39 bytes The usual +4 approach. f(X)->[tl(integer_to_list(I+4))||I<-X].  Try it online! # PowerShell, 99 bytes ([char[]]"$args"|%{"0$((@(65..90|%{[char]$_})-join'').indexof($_.tostring().toupper())+1)"})-join''  Try it online! # Acc!!, 55 bytes N-6 Count i while _/5 { Write _/10+39 Write _%10+48 N-6  Try it online! ### Explanation # Read a character, subtract 6 from its charcode, and store in the accumulator N-6 # Loop while the character was not newline (i.e. the accumulator is >= 5) Count i while _/5 { # Subtract 90 from the accumulator, divide by 10, and output as a digit Write (_-90)/10+48 # Output the accumulator mod 10 as a digit Write _%10+48 # Read the next character N-6 }  # Knight, 49 bytes ;;;;=y""=xP=i 0W>+1Lx=i+1i=y+yG+4A Gx-i 1 1 1 2Oy  Try it online! What a mess. Ungolfed: ; = y "" ; = x PROMPT ; = i 0 ; WHILE (> (+ 1 LENGTH (x)) (= i (+ 1 i)) = y (+ y (GET (+ 4 (ASCII (GET x (- i 1) 1))) 1 2)) : OUTPUT y  # Nibbles, 5.5 bytes (11 nibbles) +.$>>p+4o


(Converting to base-10 digits and dropping the first one comes-out half-a-byte (1 nibble) longer: +.$>>@~+4o).  .$             # map over each character in the input:
P         #  the string representation of
`