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

41 Answers 41

0
\$\begingroup\$

GolfSharp,(non competing) 37 bytes

s=>s.s(n=>(n<106?"0":"")+(n-96)).j();
\$\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\$

Powershell, 39 bytes

Inspired by AdmBorkBork's answer.

-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})
\$\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\$

Python - 51 49 bytes

print("".join(str(ord(i)+4)[1:]for i in input()))

Explanation:

str(ord(i)+4)[1:]for i in input()  #Called generators in Python. Used the 05AB1E answer trick for this.
"".join then combines it all into a string.
print() prints out the resulting string.

Golfed to 49 bytes because .join will accept generators too

\$\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\$
  • \$\begingroup\$ List<char> is a valid way to take in a string \$\endgroup\$ – ASCII-only Feb 24 at 21:27
  • \$\begingroup\$ I know, I just thought of the alternative after my first solution so I put it after it \$\endgroup\$ – Embodiment of Ignorance Feb 24 at 21:30
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\$
  • \$\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\$ – Jonathan Frech Feb 24 at 17:05
  • \$\begingroup\$ You should at least rename the function to a 1-character name. Also, lambdas (anonymous functions) are valid \$\endgroup\$ – ASCII-only Feb 24 at 21:28
0
\$\begingroup\$

Python 3, 47 bytes

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

Try it online!

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

Japt -m, 8 bytes

c +4 s Å

Run it online

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.