32
\$\begingroup\$

The task is to display n characters of the ASCII table.

You may write a function (or a program that takes the argument as a parameter, STDIN is allowed as well) that takes a parameter n, which will be the index of the last character to print.

The task is quite simple, so as an example here's a possible implementation in Python 2.7:

(lambda n:map(chr, range(n)))(256)

As I said it's a simple task. So this is code-golf and the shortest codes wins!

EDIT

As some of you pointed out this code doesn't print the result. It's just an example since I might struggle explaining the problem in english ;-).

EDIT2

Feel free to post the answer in any programming language, even if it's not the shortest code. Maybe there are some interesting implementations out there!

EDIT3

Fixed the example so it prints the result.

\$\endgroup\$
14
  • \$\begingroup\$ 1. Does it have to be a function? 2. According to your reference code, n would be the first character that is not printed. \$\endgroup\$
    – Dennis
    Oct 28, 2014 at 12:49
  • 2
    \$\begingroup\$ Actually the reference code prints nothing. It just returns a list of the characters and lets the REPL do whatever it wants with the result. \$\endgroup\$
    – manatwork
    Oct 28, 2014 at 12:50
  • 1
    \$\begingroup\$ Can somebody please explain the downvote? I am sorry if my english isn't that good. If there's something unclear within the question please tell me. \$\endgroup\$
    – oopbase
    Oct 28, 2014 at 12:57
  • 1
    \$\begingroup\$ for x in range(input()):print chr(x) Would actually print the characters, if you want to edit your example. \$\endgroup\$ Oct 28, 2014 at 12:59
  • 2
    \$\begingroup\$ nota [i for i in range(n)] is quite similar to range(n) \$\endgroup\$
    – njzk2
    Oct 28, 2014 at 19:22

101 Answers 101

1
\$\begingroup\$

Kotlin, 42 bytes

{n:Int->for(c in 0..n-1)print(c.toChar())}

Try it online!

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

Check, 2 bytes

,o

Try it online!

Check has:

  • Implicit input
  • A range builtin
  • A builtin to output a list of codepoints as characters
\$\endgroup\$
1
\$\begingroup\$

Momema, 25 bytes

0*-8j0j+1-=*0-9*00+-1*0j1

Outputs the characters in reverse order. Try it online!

Explanation

0   *-8     #            [0] = read num
j   0       #  label j0: goto j0
j   +1-=*0  #  label j1: goto j(1 - !![0])
-9  *0      #            print char [0]
0   +-1*0   #            [0] = 1 + [0]
j   1       #  label j2: goto j0
\$\endgroup\$
1
\$\begingroup\$

Keg, 4 bytes (SBCS)

¿ï(,

TIO

Push input, output ASCII table.

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

Japt, 7 bytes

Uo md q

Test it online!

How it works

      // Implicit: U = input integer
Uo    // Create the range [0..U).
md    // Map each item X in this range to X.d().
      // If X is a number, this turns into the character with that char code.
q     // Join the array with the empty string.
      // Implicit: output last expression
\$\endgroup\$
1
\$\begingroup\$

Snap!, 121 bytes

enter image description here

when gf clicked
set[a v]to(
ask[]and wait
for[i v]=(1)to[answer v]
set[a v]to join[a v]unicode[i v]as letter
end
say[a v]

Snap! is quite better than scratch, but I am having problem at the scratchblocks2 syntax because Snap! has some extra blocks.

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

A0A0, 122 bytes

I0A3L0S1M4V0G0D1A0
G-1A0A0
A0C4G1G1A0
A0
A0G1G1G1V0P0S1A0
A0A1G-4G-4A0
G-4

A0A0
C3G1G1A0C3G1G1A0
A0G-6A0
A0A1G-3G-3A0
G-3

Works by creating a loop where one line counts how many iterations (from x to 0) have been done and the other line prints each character (from 0 to x). The first line is constructing during run-time based on the inputted number in the first line of the code.

L0 S1 M4 V# G0 D1 ; first line, the # indicates user number
L0                ; compares 0 to the inputted number
   S1             ; adds 1 to the comparison result
      M4          ; multiplies this result with 4
         V#       ; sets this result to the G0
            G0    ; jumps this result of lines below
               D1 ; subtract one from the inputted number

G1 G1 G1 V0 P0 S1 ; second line
G1 G1 G1          ; no-ops to align the instructions with the first line
         V0       ; the operand, sets the value of the next instruction
            P0    ; prints the value set by the operand
               S1 ; adds 1 to the operand

For the jumping, jumping by 4 (we hit the last number) will jump to an empty line, halting the program, while jumping by 8 (we're not finished yet) gets us into a loop below which jumps back up to the first loop.

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

Stax, 1 byte

r

Run and debug it

Integer arrays are auto printed as strings.

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

Japt -P, 2 bytes

od

Try it

od     :Implicit input of integer
o      :Range [0,input)
 d     :Get the character at each codepoint
       :Implicitly join & output
\$\endgroup\$
1
\$\begingroup\$

Pushy, 2 bytes

Input is given on the command line, $ pushy asciitable.pshy 90. Note that this will not work for numbers larger than 127.

X"

Explanation:

    \ Implicit: Input number on stack
X   \ Get range [0, n)
 "  \ Interpret as charcodes and print
\$\endgroup\$
0
1
\$\begingroup\$

Thunno 2 B, 1 byte

L

Attempt This Online!

Thunno 2, 3 bytes

LCJ

Attempt This Online!

Explanation

L  # Implicit input
L  # Lowered range
   # B flag casts the list of ordinals
   # into a string of characters
   # Implicit output
LCJ  # Implicit input
L    # Lowered range
 C   # Cast to characters
  J  # Join into a string
     # Implicit output
\$\endgroup\$
0
\$\begingroup\$

Haskell - 16 22 characters

(`take`['\0'..])

Edit

Now printing the result

print.(`take`['\0'..])
\$\endgroup\$
3
  • \$\begingroup\$ nice trick to get rid of a character \$\endgroup\$ Oct 29, 2014 at 9:32
  • \$\begingroup\$ This doesn't actually print the result. \$\endgroup\$
    – Luna
    Oct 29, 2014 at 15:20
  • \$\begingroup\$ @JanDvorak he pretty much removed the space, as (``) does the same thing as flip here, in just as many characters... \$\endgroup\$ Oct 29, 2014 at 15:57
0
\$\begingroup\$

Lua

for i=0,255 do
    print(string.char(i))
end
\$\endgroup\$
2
  • 4
    \$\begingroup\$ This is code golf, so please include the byte count of your submission in the answer. Also, you don't need the line breaks. You can put all of this in one line like for i=0,255 do print(string.char(i)) end. \$\endgroup\$ Dec 31, 2014 at 19:19
  • \$\begingroup\$ This is not valid solution as it not handles input/parameter as required. \$\endgroup\$
    – manatwork
    Jul 22, 2016 at 9:27
0
\$\begingroup\$

PL/SQL 59

select chr(rownum)from dual connect by rownum<=&i

Didn't see a requirement that said output had to be a concatenated string, so this should work?

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

JavaScript, 65 bytes

First code golf, but here's my simple JavaScript implementation:

function f(n){for(i=1;i<n;)console.log(String.fromCharCode(i++))}

Not great but decent. ;)

Usage:

f(x) // x could be any number, but generally numbers
     // greater than 0 are better. ;)

The output is rather long so I'm not going to copy it but if you run it I think you'll get the idea. :P

Uncompressed:

function f(n) {
    for (i = 1; i < n;)
        console.log(String.fromCharCode(i++)); // by using i++ we increase
                                               // i right as we get it
}
\$\endgroup\$
4
  • \$\begingroup\$ Welcome to PPCG! I don't think you need those braces, and you do the i++ when call the function like ...fromCharCode(i++) and then leave the third section of the for statement empty. \$\endgroup\$ Jun 10, 2015 at 13:42
  • \$\begingroup\$ However, I just noticed that your answer isn't quite valid. You should write a function which takes 256 as a parameter. \$\endgroup\$ Jun 10, 2015 at 13:44
  • \$\begingroup\$ Also, I think you should start the loop at 0 rather than 1. \$\endgroup\$ Jun 10, 2015 at 14:00
  • \$\begingroup\$ Ah, I'll update to fit what you guys have pointed out. :) \$\endgroup\$
    – Nebula
    Jun 10, 2015 at 15:09
0
\$\begingroup\$

C#, 112 | 83 bytes

Full program, 112 bytes:

class P{static void Main(string[]a){for(int i=0;i<int.Parse(a[0]);i++){System.Console.Write("{0}\n",(char)i);}}}

A function, 83 bytes:

static void f(int n){for(int i=0;i<n;i++){System.Console.Write("{0}\n",(char)i);}}}
\$\endgroup\$
0
\$\begingroup\$

Microscript, 9 bytes

i1{d1s}ah

Or alternatively:

i1cs1]fah

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

C, 31 bytes

i;f(n){for(;i<n;)putchar(i++);}

Use:

i;f(n){for(;i<n;)putchar(i++);}

int main(void)
{
    f(34);   //Call the function
    return 0;
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ The challenge says to output all ASCII characters, not just the printable ones, and also to print "n ACII characters", not "ASCII characters up to n", so you could save 4 characters by letter i default to 0 and changing the loop condition to <. And you could save 1 (or 2, depending how eager you are to invoke undefined behavior) character by moving the call to putchar() into the loop condition. \$\endgroup\$
    – Stuntddude
    Jun 12, 2015 at 0:26
  • \$\begingroup\$ Thanks. Does the program look ok now? \$\endgroup\$
    – Spikatrix
    Jun 12, 2015 at 4:42
  • \$\begingroup\$ @Stuntddude In whch way moving the putchar inside the loop condition would trigger UB? \$\endgroup\$
    – edc65
    Jun 12, 2015 at 10:16
0
\$\begingroup\$

Excel VBA - 143

For i = 1 To 127
Cells(i, 1) = i
Cells(i, 2) = Evaluate("DEC2HEX(" & i & ")")
Cells(i, 3) = Evaluate("DEC2OCT(" & i & ")")
Cells(i, 4) = Chr(i)
Next
\$\endgroup\$
0
\$\begingroup\$

K: 8 bytes

{10h$!x}

Q: 11 bytes

{10h$til x}
\$\endgroup\$
0
\$\begingroup\$

Gema, 43 characters

*=@set{i;0}@repeat{*;@int-char{$i}@incr{i}}

Sample run:

bash-4.3$ gema '*=@set{i;0}@repeat{*;@int-char{$i}@incr{i}}' <<< 96 | xxd 
0000000: 0001 0203 0405 0607 0809 0a0b 0c0d 0e0f  ................
0000010: 1011 1213 1415 1617 1819 1a1b 1c1d 1e1f  ................
0000020: 2021 2223 2425 2627 2829 2a2b 2c2d 2e2f   !"#$%&'()*+,-./
0000030: 3031 3233 3435 3637 3839 3a3b 3c3d 3e3f  0123456789:;<=>?
0000040: 4041 4243 4445 4647 4849 4a4b 4c4d 4e4f  @ABCDEFGHIJKLMNO
0000050: 5051 5253 5455 5657 5859 5a5b 5c5d 5e5f  PQRSTUVWXYZ[\]^_
\$\endgroup\$
0
\$\begingroup\$

Hassium, 45 Bytes

func a(n)for(x=0;x<n;print((x++).toChar())){}

Run and see expanded here

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

Pylons, 9

0{d1+,i}c

How it works:

0     # Push 0 to the top of the stack.
{     # Start a for loop.
 d    # Duplicate the top of the stack.
  1   # Push 1 to the stack.
   +  # Add the top two elements of the stack.
 ,    # Switch to for loop iterators.
  i   # Get input
   }  # End for loop
c     # Print stack as joined array of chars and exit.
\$\endgroup\$
0
\$\begingroup\$

Oracle SQL 11.2, 48 bytes

SELECT CHR(LEVEL)FROM DUAL CONNECT BY LEVEL<=:1;
\$\endgroup\$
0
\$\begingroup\$

DUP, 21 bytes

[1+a:0[$1+$a;<][^,]#]

Try it here!

Anonymous lambda. Usage:

256[1+a:0[$1+$a;<][^,]#]!

Explanation

[                     {start lambda}
 1+a:                 {inputnum is end of range (saved to a)}
     0                {0 is start of range}
      [       ][  ]#  {while loop}
       $1+$a;<        {dup, increment, check if top of stack < a}
                ^,    {if so, output char}
                    ] {end lambda}
\$\endgroup\$
0
\$\begingroup\$

Perl 6, 9 bytes

(^*).chrs

The header and code alone are too short, so have this extra sentence.

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

Java 76 74 70

Thanks to Mego for saving 4 bytes.

public void f(int x){for(int u=0;u<x;u++)System.out.println((char)u);}

Previous versions:

class f{public void f(int x){for(int u=0;u<x;u++)System.out.println((char)u);}}
class f{public f(int x){for(int u=0;u<x;u++)System.out.println((char)u);}}

The second version needs to be called by new f(n);

Feel free to give me ideas for making it even smaller.

\$\endgroup\$
2
  • \$\begingroup\$ You can do a standalone function: public void f(int x){...}. For even less bytes, if you're working with Java 8, you can use a lambda: x->java.util.stream.IntStream.range(0,x).mapToObj(n->(char)n); \$\endgroup\$
    – user45941
    Dec 14, 2016 at 14:44
  • \$\begingroup\$ In fact, you don't even need the public - a default-scoped function is fine. \$\endgroup\$
    – user45941
    Dec 19, 2016 at 5:32
0
\$\begingroup\$

QBIC, 14 bytes, NC

:[a|?chr$(b-1)

Takes a number form the command line and loops from 1 to that number, feeding each iteration into CHR$.

Non-competing, QBIC was thought of a year after this challenge was posted.

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

Java 8, 45 bytes

A lambda expression which takes an integer and prints the table to STDOUT. Golfing suggestions welcome!

x->{for(char i=0;i<x;System.out.print(i++));}

It's not clear whether OP wants x to be the last character printed, or the first character not printed. If it's the second, just change i<x to i<=x :)

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

Python, 30 bytes

An unnamed lambda function which returns the result as a string.

lambda n:('%c'*n)%(*range(n),)

Try it online!

This uses the printf-style string formatting: when '%c' is formatted with an integer, it converts the integer to a character and displays that. So, we multiply the string by n, and format it with all values in range(n).

29 bytes

If we want to be cheeky and return a list of characters instead of a string, we can get 1 byte shorter:

lambda n:[*map(chr,range(n))]

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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