28
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Related. \$\endgroup\$ Commented Oct 28, 2016 at 17:09

64 Answers 64

1
\$\begingroup\$

Labyrinth, 40 bytes

 ,")@ !{_10%! ( / _ 01_}:-69" 
\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Oct 29, 2016 at 10:26
  • \$\begingroup\$ @Billywob Thanks! For the padding, I tried using formatC earlier but that worked out as needing one more byte than the current approach. \$\endgroup\$ Commented Oct 29, 2016 at 14:27
1
\$\begingroup\$

Actually, 10 bytes

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

O4+`$pX`MΣ 

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. 
\$\endgroup\$
1
\$\begingroup\$

Python 3, 47 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Python 2, 51 49 45 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 ) + 4 Added by 4 ` [1:] With the first character removed ) for i in x While a char i is in the string x 
\$\endgroup\$
1
\$\begingroup\$

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 
\$\endgroup\$
1
\$\begingroup\$

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 $ / convert to string ,// / flatten (,/) over (/) 
\$\endgroup\$
1
\$\begingroup\$

Google Sheets, 76 bytes

=ArrayFormula(Join(,Text(Code(Mid(A1,Row(Indirect("1:"&Len(A1))),1))-96,"00" 

Sheets will automatically add three closing parentheses when you exit the cell.
Input is in cell A1

Indirect("1:"&Len(A1)) returns a range as tall as the input is long.
Row(Indirect(~)) returns the row numbers, so it's a list of numbers from 1 to the input length.
Mid(A1,Row(~),1) returns each character from the input, one at a time.
Code(Mid(~))-96 returns the ASCII code for each character, down-shifted so a = 1.
Text(Code(~),"00") pads the result above to two digits.
Join(,Text(~)) joins all those padded results without a delimiter.
ArrayFormula(Join(~)) makes all the stuff above operate on arrays. Without, the result would just be the first padded alphabet code: helloworld would return 08.

Example

\$\endgroup\$
1
\$\begingroup\$

Labyrinth, 27 bytes

 ,_9 @ %! 6-: 0 _ 1_!/01 

Try it online!

How it works

 Start at the first valid instruction, going right ,_96-: Push a char, subtract 96, duplicate @ If it's negative (EOF), turn left and terminate Otherwise it is positive. Turn right and follow the cycle _10/! Integer-divide by 10, pop and print as integer _10%! Modulo 10, pop and print as integer Re-enter the start of the program, looping until EOF 
\$\endgroup\$
2
  • \$\begingroup\$ By replacing the second _1 with # and some careful twisting of the code, this can be 25 bytes \$\endgroup\$ Commented Sep 30, 2020 at 11:41
  • \$\begingroup\$ Oh, I realise that the # change did nothing, aince I have to add a no-op. It was just the rearrangement of the code that golfed bytes \$\endgroup\$ Commented Sep 30, 2020 at 14:24
1
\$\begingroup\$

Powershell, 39 33 bytes

Inspired by AdmBorkBork's answer.

-6 bytes thanks @Julian

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

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 33 bytes using splatting ;) \$\endgroup\$ Commented Apr 8, 2021 at 2:30
1
\$\begingroup\$

Alice, 40 bytes

wi.hn$@'`-v 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 
\$\endgroup\$
1
\$\begingroup\$

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 ;$x Else, return the number converted to string ,/ Join 
\$\endgroup\$
1
\$\begingroup\$

Nibbles, 5.5 bytes (11 nibbles)

+.$>>`p+4o 

Another port of Adnan's 05AB1E answer.
(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: >> # drop the first element of `P # the string representation of o # the ASCII value +4 # plus 4 + # and finally concatenate it all 

enter image description here

\$\endgroup\$
0
\$\begingroup\$

Groovy, 51 Bytes

{it.collect{(((int)it-96)+"").padLeft(2,"0")}.join()} 
\$\endgroup\$
0
\$\begingroup\$

Befunge-98, 19 bytes

#@~'`-:a/'0+,a%'0+, 
\$\endgroup\$
0
\$\begingroup\$

Groovy - 31 bytes

Groovy conversion of NumberKnot's solution in java:

{it.each{printf("%02d",it-96)}}

Example here using various options:

http://ideone.com/vd0dTX

\$\endgroup\$
0
\$\begingroup\$

Pyke, 7 bytes

F.oOO`t 

Try it here!

F - for i in input: .o - ord(i) OO - ^ + 4 ` - str(^) t - ^[1:] - sum(^) 
\$\endgroup\$
0
\$\begingroup\$

C#, 54 bytes

s=>String.Join("",s.Select(n=>(n<106?"0":"")+(n-96))); 
\$\endgroup\$
0
\$\begingroup\$

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")

\$\endgroup\$
0
\$\begingroup\$

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!

Try it here with ideone.

\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
3
  • \$\begingroup\$ List<char> is a valid way to take in a string \$\endgroup\$ Commented Feb 24, 2019 at 21:27
  • \$\begingroup\$ I know, I just thought of the alternative after my first solution so I put it after it \$\endgroup\$ Commented Feb 24, 2019 at 21:30
  • \$\begingroup\$ 32 bytes with SelectMany (but returns an IEnumerable<char>). \$\endgroup\$ Commented Apr 24, 2020 at 14:52
0
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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? \$\endgroup\$ Commented Feb 24, 2019 at 17:05
  • \$\begingroup\$ You should at least rename the function to a 1-character name. Also, lambdas (anonymous functions) are valid \$\endgroup\$ Commented Feb 24, 2019 at 21:28
0
\$\begingroup\$

Japt -m, 8 bytes

c +4 s Å 

Run it online

\$\endgroup\$
0
0
\$\begingroup\$

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 
\$\endgroup\$
0
\$\begingroup\$

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 
\$\endgroup\$
0
\$\begingroup\$

Erlang (escript), 39 bytes

The usual +4 approach.

f(X)->[tl(integer_to_list(I+4))||I<-X]. 

Try it online!

\$\endgroup\$
0
\$\begingroup\$

PowerShell, 99 bytes

([char[]]"$args"|%{"0$((@(65..90|%{[char]$_})-join'').indexof($_.tostring().toupper())+1)"})-join'' 

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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 } 
\$\endgroup\$
0
\$\begingroup\$

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 
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.