43
\$\begingroup\$

Given a string as input, output a number of whitespace characters (0x0A and 0x20) equal to the length of the string.

For example, given the string Hello, World! your code would need to output exactly 13 whitespace characters and nothing else. These can be any mix of spaces and newlines.

Your code should not output any additional trailing newlines or spaces.

Testcases:

     Input      -> Amount of whitespace to output
"Hello, World!" -> 13
"Hi"            -> 2
"   Don't
Forget about
Existing
Whitespace!   " -> 45
""              -> 0
"             " -> 13
"
"               -> 1

Scoring:

This is so fewest bytes wins!

\$\endgroup\$
11
  • 1
    \$\begingroup\$ I don't get what you mean with that “0x0A”. Where should that be output? Should that be kept, so “a␠b␊c” becomes “␠␠␠␊␠”? \$\endgroup\$
    – manatwork
    May 25 '17 at 12:56
  • 1
    \$\begingroup\$ @manatwork 0x0A and 0x20 are the hexadecimal values for the Newline and Space characters respectively \$\endgroup\$
    – Mayube
    May 25 '17 at 12:58
  • 1
    \$\begingroup\$ “output a number of whitespace characters (0x0A and 0x20)” – Where in the output should those newline characters be? \$\endgroup\$
    – manatwork
    May 25 '17 at 13:00
  • 3
    \$\begingroup\$ These can be any mix of spaces and newlines Your output can be any mix of spaces and newlines, you can just output spaces if you want, like everyone else, or you can just output newlines. It's up to you \$\endgroup\$
    – Mayube
    May 25 '17 at 13:05
  • 1
    \$\begingroup\$ Got it. Thanks. \$\endgroup\$
    – manatwork
    May 25 '17 at 13:06

122 Answers 122

2
\$\begingroup\$

Cascade, 14 11 9 8 bytes

? .'
,^\

Try it online!

-1 byte thanks to Jo King realizing it's shorter to just turn into two lines

Since the program never uses the return values of anything other than , and ', and the order spaces are printed in doesn't matter (since they're indistinguishable), ^ can be used backwards: instead of printing a space then recurring, this recurs then prints a space.

Ungolfed, this looks something like:

 @
 ?
 |\
 , \
    ^
   / \
  /   .
 |    \
 |     '
 |

If tabs are legal whitespace alongside spaces and newlines:

Cascade, 7 bytes

? .
,^9

Try it online!

thanks to Jo King.

\$\endgroup\$
1
  • \$\begingroup\$ Note that this uses ? over _ because end of input returns -1, and ? tests for a positive center, but _ only tests for a left not equal to 0. \$\endgroup\$ Nov 16 '19 at 22:26
2
\$\begingroup\$

Gol><>, 5 bytes

iEH~a

Try it online!

Explanation

i      Push 1 char from the input to the stack
 E     If EOF was reached execute next, else skip 1
  H    Output the entire stack as chars and halt
   ~a  Discard the last read char and push 0x0a to the stack

Since Gol><> is actually a 2D language the programm counter loops back to the other side when it hits a border. This is used to loop back to the beginning after the discard & 0x0a push step.

\$\endgroup\$
2
\$\begingroup\$

Labyrinth, 6 bytes

 ,
\"@

Try it online!

A character input command, a newline-printing command, a halt command, and a three-way junction for branching. I don't think we can do better than that. (A conditional halt without a three-way junction should involve division by zero, which gets unnecessarily verbose)

    Start at the first valid command
,   Take a character input (-1 on EOF)
"   A no-op at 3-way junction: turn left if negative, turn right if positive
    (we can assume the input string consists of printable ASCII, which is > 0)

\   If positive: print a newline, turn backwards
"   3-way junction: top > 0, so turn right (which is reflected to left by a wall)
,   Repeat from the start of the program until it hits EOF

@   If EOF, halt
\$\endgroup\$
2
\$\begingroup\$

GolfScript, 3 bytes

,n*

Try it online!

pretty self explanatory, and I don't think there is a way to make it smaller unless there is some really weird thing with % or /.

\$\endgroup\$
2
  • \$\begingroup\$ It's not “self-explanatory” for those who don't know GolfScript… \$\endgroup\$
    – xigoi
    Nov 17 '20 at 16:43
  • \$\begingroup\$ @xigoi well, it is as far as golfscript programs go. The comma gets the length of the input, the n is a newline, and the * repeats the string. \$\endgroup\$ Nov 18 '20 at 14:00
1
\$\begingroup\$

SmileBASIC 3, 22 bytes

Asks for a string from the console as input, then prints length spaces. PRINT (or ? here) adds a trailing newline by default, so we use the ; to disable it.

LINPUT A$?" "*LEN(A$);
\$\endgroup\$
1
  • \$\begingroup\$ This technically isn't valid since LINPUT doesn't accept newlines, so you'll have to define a function (also ; isn't required because PRINT doesn't really output newlines) \$\endgroup\$
    – 12Me21
    Mar 3 '18 at 16:18
1
\$\begingroup\$

Mathematica, 30 bytes

Row@Table[" ",StringLength@#]&
\$\endgroup\$
1
\$\begingroup\$

Lua, 25 bytes

for i=1,#...do
print()end

Same length as:

io.write((' '):rep(#...))

Try it online!

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

tcl, 19

regsub -all . $s \ 

demo

To test it, click "Run it" button and then select the white space on the white bottom area. A better test is to add a space and a letter before the ] as I describe:

puts [regsub -all . $s \  x]    
                        ^^ Two spaces here

and it will output the count of characters of each string exactly equal to the ones on the question.

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

Rust, 23 bytes

|s|" ".repeat(s.len());

First time using Rust so not 100% sure I've got everything correct, let me know if I need to change anything. I couldn't work out how to test this, as I'm still new to it, but judging from the documentation it should work. Also any improvements are more than welcome!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Hi fellow newbie! I tried to rig up enough surrounding code to make a Try It Online link for this answer (and any similar Rust entries). I did get somewhere but I needed to add a type declaration at the cost of an extra 5 bytes (for a total of 28 bytes) to get it to compile. \$\endgroup\$
    – RJHunter
    May 26 '17 at 3:48
1
\$\begingroup\$

Brain-Flak, 28 bytes

{{}<>((()()()()()){})<>}<>{}

Try it online!

{{}                    }     # For every input character...
     ((()()()()()){})        #    Push 10...
   <>                <>      #    on the other stack
                        <>   # Switch to the stack with all of the newlines
                          {} # Pop a newline because the interpreter prints a newline :(
\$\endgroup\$
1
\$\begingroup\$

pb - 17 bytes

^w[B!0]{>}<vb[32]

Goes to the last character of the input and puts a space on the canvas cell representing it. Because output in pb is 2D, the empty cells before it are automatically filled in with spaces when it's outputted.

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

x64 ASSEMBLY (linux nasm) - 131 bytes

mov r8, [rsp+16]
mov rdi, 1
mov rdx, 1
mov rax, 1
mov rsi,n
l:syscall
inc r8
cmp byte [r8],0x00
jnz l
mov rax,60
syscall
n: db " "

build and run with:

nasm -felf64 invisible_golfed.asm
ld invisible_golfed.asm
./a.out

This will give the warning

ld: warning: cannot find entry symbol _start; defaulting to 00000000000400080

warning free version below

without warnings - 152

global _start
_start:mov r8, [rsp+16]
mov rdi, 1
mov rdx, 1
mov rax, 1
mov rsi,n
l:syscall
inc r8
cmp byte [r8],0x00
jnz l
mov rax,60
syscall
n: db " "
\$\endgroup\$
4
  • \$\begingroup\$ When I give the program "When I give the program" on STDIN, it only prints a single space. I suggest your read the I/O defaults and revise your answer. Essentially, revise your code to take input from STDIN. \$\endgroup\$ May 26 '17 at 3:15
  • \$\begingroup\$ My bad I have it reading from the command line. I'll fix that tomorow when I get the chance. \$\endgroup\$
    – Samuel
    May 26 '17 at 4:46
  • 1
    \$\begingroup\$ Your byte-counting is not correct. With assembly language, we count th bytes of the machine code that is generated, not the characters in the instruction mnemonics. Beyond that problem, there's lots of room for optimization here. Save several bytes by changing instructions of the form MOV Rxx, x to MOV Exx, x, where x is an immediate value <= 32 bits, taking advantage of the fact that the upper 32-bits of a 64-bit register are implicitly cleared. I also don't know why you define n separately, instead of embedding it into the instruction: MOV ESI, ' '. That gets me down to 48 bytes. \$\endgroup\$
    – Cody Gray
    May 26 '17 at 12:01
  • \$\begingroup\$ You can save even more by changing all but the first instruction in the MOV reg, 1 sequence to a reg-reg move, which has a much shorter encoding: mov eax, 1+mov edx, eax+mov edi, eax. That's only 36 bytes. \$\endgroup\$
    – Cody Gray
    May 26 '17 at 12:04
1
\$\begingroup\$

C, 57 33 30 bytes

-3 thanks to Tas.

f(int*a){while(*a++)puts("");}

Try it online!

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

PowerShell, 18 bytes

' '*"$args".Length

Try it online!

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

Python, 23 bytes

print(''*len(input()))

First time

\$\endgroup\$
3
  • \$\begingroup\$ Assuming where input comes from is usually frowned upon here. You need to wrap the code in a program/method or ask the user for input. \$\endgroup\$ May 26 '17 at 8:25
  • \$\begingroup\$ @TheLethalCoder fixed! \$\endgroup\$
    – spark
    May 26 '17 at 8:31
  • \$\begingroup\$ You don't need the space between , and ' in your second version. However, that second version isn't a full program - you need a print call, and you need to fix your str.replace call (not enough arguments). \$\endgroup\$
    – user45941
    May 26 '17 at 8:50
1
\$\begingroup\$

Lua, 17 Bytes

s=s:gsub("."," ")

Simple regular expression substitution, replaces any character found with a space.

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

Z80Golf, 9 bytes

00000000: d5cd 0380 3001 763e 20                   ....0.v>

Try it online!

Disassembly:

  push de     ; push $0000
  call $8003  ; getchar(A)
  jr nc, k    ; jump if not EOF
  halt
k:ld a, ' '   ; replace A with space
              ; memory from $000a through $7fff is $00=NOP...
              ; PC reaches $8000=putchar(A) and RETs to pushed address.

(If you remove the last two bytes, this is a cat program.)

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

dc, 12 bytes

Z256r^25.5/P

Try it online!

Takes input from the stack, outputs newlines to stdin.

I know there's two dc answers here already, but this one uses a different approach, with math! Plus it's 6 bytes shorter, so I guess that's all right.

Explanation

One of dc's three explicit printing commands is P, which takes a number and outputs it as a base 256 (technically base UCHAR_MAX+1, works on my machine) byte stream. So I need to feed it the number (where n is the length of the given string, and 10 is the codepoint of the linefeed character):

   10*256^(n-1) + 10*256^(n-2) + ... + 10
 = 10 * (256^(n-1) + 256^(n-2) + ... + 1)   (factoring out 10)
 = 10 * (256^n - 1) / (256 - 1)             (geometric series formula)
 = (256^n - 1) / 25.5                       (combining constants)    
~= (256^n) / 25.5                           (because dc's default precision is 0)

The code is a straightforward calculation of this number, followed by P.

\$\endgroup\$
2
  • \$\begingroup\$ Argh, doesn't handle the empty string (P outputs a NUL when it pops a 0). I guess I could say it's outputting a null-terminated string...seems like a stretch though. \$\endgroup\$ Jul 10 '18 at 19:43
  • \$\begingroup\$ Also, it's only on TIO that you can tell. On my machine a NUL gets eaten silently so it works. I still feel badly about it. \$\endgroup\$ Jul 10 '18 at 19:57
1
\$\begingroup\$

Octave / Matlab, 25 23 bytes

@(x)repmat(' ',size(x))

Saved 2 bytes thanks to Giuseppe

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ I think the f= is unnecessary; anonymous functions / function handles are perfectly acceptable on PPCG. \$\endgroup\$
    – Giuseppe
    Jul 10 '18 at 22:27
  • \$\begingroup\$ You can also include a link to Try it online! for Octave so others can test your answer. \$\endgroup\$
    – Giuseppe
    Jul 10 '18 at 22:28
1
\$\begingroup\$

Java (JDK), 84 bytes

static String m(String n){int i=0;String d="";while(i++<n.length())d+=" ";return d;}

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Updated Sir @JoKing. \$\endgroup\$ Oct 22 '18 at 12:08
  • \$\begingroup\$ This does not address my other concerns, i.e. initialising d and reusing the function. Also, wouldn't it be shorter to use an anonymous lambda? \$\endgroup\$
    – Jo King
    Oct 22 '18 at 12:14
  • \$\begingroup\$ Your suggestions are so useful, Thanks @JoKing \$\endgroup\$ Oct 23 '18 at 5:07
1
\$\begingroup\$

Wren, 22 bytes

Multiply the space by the length of the string.

Fn.new{|a|" "*a.count}

Try it online!

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

Brain-Flak, 28 bytes

{}{{}<>((()()()()()){})<>}<>

Try it online!

{}                  pop one input character (because Brain-Flak always outputs a trailing newline
{                   for each input character
  {}                pop that character
  <>                switch to other stack
  ((()()()()()){})  push 10 (newline)
  <>                back to input stack
}
<>                  switch to other stack. This is printed implicitly when the program ends
\$\endgroup\$
1
\$\begingroup\$

Keg,-lp -ir, 3 bytes

( ,

Try it online!

This takes input as characters and prints a space for each character The -lp flag makes the length() function take input if the stack is empty and the -ir flag ensures that the implicit input is as characters.

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

x86-16 ASM, IBM PC DOS, 11 bytes

Binary:

00000000: 8a0e 8000 49b8 200a cd10 c3              ....I. ....

Unassembled:

D1 EE       SHR  SI, 1          ; SI to 80H (SI intialized at 100H) 
AC          LODSB               ; load string length into AL
91          XCHG AX, CX         ; put input string length into CX
49          DEC  CX             ; remove leading whitespace from length
AC          LODSB               ; load whitespace delimiter into AL
B4 0A       MOV  AH, 0AH        ; BIOS "write character CX number of times" function
CD 10       INT  10H            ; call BIOS, display to console
C3          RET                 ; return to DOS

Explanation:

Input is via command line, though all that's important is the length. Command line input length is always stored at memory address DS:0080H in DOS, so put that into CX. DOS includes the space between the executable name and the command line args string in this number.

For example: in FOO.COM Hello, length is 6 and command line string is " Hello", or calling as FOO.COM/Hello, command line string is "/Hello" (Note: those are the the only valid characters for the character immediately after the executable name). This first character (will be a space when called normally) is what is displayed as the "invisible text" for output. This builds in a handy little "debug mode" where you can use a slash instead of a space to actually be able to test your output is the right length.

Then, use the IBM PC BIOS's INT 10H "Write character only at cursor position" (0AH) function that writes the same character CX number of times.

Example Output:

Admittedly, displaying 13 chars of whitespace is not very interesting in a screenshot. However, by using a slash instead of a space ("debug mode") you can actually see that you are displaying the right number of chars.

enter image description here

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

naz, 40 bytes

2a2x1v1x1f1r3x1v2e0m4a8m1o1f0x1x2f0a0x1f

Works for any input string terminated with the control character STX (U+0002).

Explanation (with 0x commands removed)

2a2x1v                 # Set variable 1 equal to 2
1x1f1r3x1v2e           # Function 1
                       # Read a byte of input
                       # Jump to function 2 if it equals variable 1
            0m4a8m1o1f # Otherwise, output a space and jump back to the start of function 1
1x2f0a                 # Function 2
                       # Add 0 to the register
1f                     # Call function 1
\$\endgroup\$
1
\$\begingroup\$

GolfScript, 5 bytes

Port of CJam answer.

," "*

Try it online!

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

Ahead, 12 bytes

S0d3-' \k:W@

Try it online!

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

Python 3, 23 bytes

print(' '*len(input()))

Try it online!

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

Java, 24 bytes

s->s.replaceAll("."," ")

TIO

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

Pyth, 4 bytes

*dtl

Try it online!

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