Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

The program

Given a number, program must return the alphabet letter correspondent.

Rules

  • The challenge here is to make it as short as possible, everyone knows it is a very easy program to do.
  • The number is not zero-based, it means 1 is A, 2 is B, 3 is C and so it goes...
  • Any language will be accepted.
share|improve this question
I suspect the winning answer will be tiny. – David Carraher Sep 14 '12 at 2:53
I think my answer won – dspyz Sep 18 '12 at 1:01

14 Answers

up vote 2 down vote accepted

DC - 6 characters

Full program including input and output.

?64+af

save to file and run with $ dc file

share|improve this answer

C, 4 chars

x+64

plus I must pad the body of this answer...

share|improve this answer

APL (4)

(Full program)

⎕⌷⎕A

Explanation:

(user input) (index) ⎕A (alphabet)

(They're supposed to be boxes, it's not an encoding problem.)

share|improve this answer

Brainfuck, 107

>,----------[>++++++[-<------>],----------]<[<]>>[<--[->++++++++++<]>>]+++++++[-<+++++++++>]<-.>++++++++++.
share|improve this answer

J, 7 characters

a.{~64+

Usage:

   a.{~64+1
A
share|improve this answer

Python 9

chr(64+x)

Or, reading from stdin and printing to stdout: 21

print chr(64+input())
share|improve this answer

R, 11 characters

LETTERS[x]

Usage:

LETTERS[21]
[1] "U"
share|improve this answer

q/k (7)

As partially applied composition:

10h$64+
share|improve this answer

Ruby 1.9 (10)

(x+64).chr

Reading from STDIN and printing to STDOUT:

$><<(gets.to_i+64).chr
share|improve this answer

Burlesque (6 characters)

Assuming the number is already on the stack:

64.+L[
(see here in action.).

Does the usual: Add 64, convert to character based on ASCII value.

If number is supplied as a string via stdin to stdout (10 characters):

ri64.+L[sh

Alternative version without using ASCII value conversion:

'@'Zr@\/!!
share|improve this answer

PHP 442

$alphabet = Array('A' => 1,'B' => 2,'C' => 3,'D' => 4,'E' => 5,'F' => 6,'G' => 7,'H' => 8,
'I' => 9,'J' => 10,'K' => 11,'L' => 12,'M' => 13,'N' => 14,'O' => 15,'P' => 16,'Q' => 17,
'R' => 18,'S' => 19,'T' => 20,'U' => 21,'V' => 22,'W' => 23,'X' => 24,'Y' => 25,'Z' => 26
)

function testInputForAlphabetCharacter($input){
    for($index = 0; $index < count($alphabet); $i = $i + 1){
        if($alphabet[$index] == $input){
            echo($index);
        }
    }
}


$input = $_GET['input'];
testInputForAlphabetCharacter($input);
share|improve this answer

Javascript, 25 chars

String.fromCharCode(66-x);

PHP, 22 chars

echo chr(66-$argv[0])
share|improve this answer

perl

chr(($ARGV[0] + 64))

Verification :

risk@skynet:~/perl$ for x in {1..26}; do perl ./ord.pl $x; done;
ABCDEFGHIJKLMNOPQRSTUVWXYZrisk@skynet:~/perl$
share|improve this answer

GolfScript, 7

64+]''+

Commentary

64    # corresponds to '@' in ASCII (65 is 'A')
+]    # add the input to 64. ']' is used for ASCII.
''+   # the conversion process

1 corresponds to A

26 corresponds to Z

share|improve this answer
It can indeed: assuming that the only thing on the stack is the number from 1 to 26, you don't need the [. – Peter Taylor Sep 18 '12 at 12:14
@PeterTaylor Okay, thank you! – Mike Dtrick Sep 18 '12 at 20:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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