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!

  • 1
    I don't get what you mean with that “0x0A”. Where should that be output? Should that be kept, so “a␠b␊c” becomes “␠␠␠␊␠”? – manatwork May 25 '17 at 12:56
  • 1
    @manatwork 0x0A and 0x20 are the hexadecimal values for the Newline and Space characters respectively – Skidsdev May 25 '17 at 12:58
  • 1
    “output a number of whitespace characters (0x0A and 0x20)” – Where in the output should those newline characters be? – manatwork May 25 '17 at 13:00
  • 1
    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 – Skidsdev May 25 '17 at 13:05
  • 1
    Got it. Thanks. – manatwork May 25 '17 at 13:06

93 Answers 93

Whitespace, 311 150 77 68 65 46 41 38 bytes

-3 bytes thanks to Kevin Cruijssen
-27 bytes thanks to Ephphatha


  
   	 	 
 
  
 	
	 			
	  
	
  
 


Try it online!

A visible format

'\n  \n   \t \t \n \n  \n \t\n\t \t\t\t\n\t  \n\t\n  \n \n\n'

Explanation (s = space, t = tab, n = new line)

nssn     # label(NULL) - loop start
ssststsn # push 10 in the stack -> [10]
sns      # duplicate the top of the stack -> [10, 10]
sns      # duplicate the top of the stack -> [10, 10, 10]
tnts     # read a single char from input, pop the stack and store at that address -> [10, 10] [10:<char that was read>]
ttt      # pop the stack and put the value at that adress on stack -> [10,<char>] [10:<char>]
ntssn    # jump to label(0) - since label(0) is not defined, the interpreter jumps to the end of the program - if the top of the stack (char) is 0 -> [10] [10:<char>]
tnss     # pop the top of the stack and print as ascii -> [] [10:<char>]
nsnn     # go back to the label(NULL)
  • 23
    Assuming this does actually work, this definitely wins my vote for most creative answer – Skidsdev May 25 '17 at 14:25
  • 22
    Wait where is the answer? Is it invisible too? – Erik the Outgolfer May 25 '17 at 14:27
  • 15
    WHAT BLACK MAGIC IS THIS. Your code is not even there! -1 – Christopher May 25 '17 at 14:32
  • 25
    @Christopher more like WHITEspace MAGIC – Rod May 25 '17 at 14:34
  • 11
    I knew someone would answer this question with a whitespace program – Draco18s May 25 '17 at 21:06

Japt, 1 byte

ç

Try it online!

  • 21
    Does japt seriously have a builtin for this? Damn... – Skidsdev May 25 '17 at 13:06
  • 19
    @Mayube well it has a builtin to replace all characters in a string with another, and the default replacement is a space ;) – Tom May 25 '17 at 13:12
  • 4
    Very nice! For those running the program, you can add the -Q flag into the input to put quotes around the output. TIO – Oliver May 25 '17 at 13:57

Haskell, 7 bytes

(>>" ")

Try it online! Usage: (>>" ") "Hello, world!".

Given two lists (and strings are lists of characters in Haskell) the >> operator will repeat the second list as many times as the first list has elements. Setting " " as second argument means we concatenate as many spaces as the input string is long.


Alternative (same byte count):

(' '<$)

Try it online! Usage: (' '<$) "Hello, world!".

Given some value and a list, the <$ operator replaces each value in the list with the given value. Thus 5 <$ "abc" results in [5,5,5], and ' ' <$ "abc" in " ".

The function can equivalently be written as (<$)' ', in case you want to find some more marine creatures in my code.

  • 15
    Its like an adorable little finless fish – Taylor Scott May 25 '17 at 13:27

brainfuck, 18 bytes

++++++++++>,[<.>,]

Try it online!

Prints one newline for each byte of input. Printing spaces instead would add 4 bytes.

  • 1
    For posterity: Mika Lammi posted a clever 16-byte answer that got buried. ,[>++++++++++.,] – Lynn Jun 23 at 14:03

Python, 19 bytes

lambda s:' '*len(s)

Retina, 3 4 bytes

S\`.

Old version, doesn't work because Retina prints a trailing line feed.

.
 

(The second line contains a space).

  • 2
    The retina TIO is quite easy to use. Here is your answer – Digital Trauma May 25 '17 at 17:44
  • 1
    Unfortunately, Retina prints a trailing linefeed by default. You'll need to prepend \` to avoid that. Then it's shorter to use S\`. though, which replaces each character with a linefeed (because it splits the input around each character). – Martin Ender Jun 7 '17 at 9:54
  • @MartinEnder Ahhh wasn't sure if that was a Retina or TIO thing. Thanks for the help on saving a byte there though! – TheLethalCoder Jun 7 '17 at 9:57

sed, 7 bytes

s/./ /g

Try it online!

C#, 28 24 bytes

s=>"".PadLeft(s.Length);

Old version using the string constructor for 28 bytes:

s=>new string(' ',s.Length);
  • 3
    Wanted to do exactly the same – LiefdeWen May 25 '17 at 12:59
  • 1
    @StefanDelport Gotta be quick with C# when I'm around :) There's Linq approaches to do the same but they're all a LOT longer... – TheLethalCoder May 25 '17 at 13:00

Retina, 5 bytes

\`.
¶

Try it online! Changes everything into newlines. The \` suppresses the extra newline Retina would normally output.

Mathematica, 21 bytes

StringReplace[_->" "]
  • 1
    If charlist input was allowed, this could be #/._->" "&. Sadly, the input is a string and Characters[] makes it one byte longer than your solution :( – CalculatorFeline May 26 '17 at 3:03
  • 1
    Doesn't this need a # and a & in it? E.g. StringReplace[#,_->" "]& – Ian Miller May 26 '17 at 3:58
  • 3
    @IanMiller Not in Mathematica 10.4 or 11. reference.wolfram.com/language/ref/StringReplace.html – alephalpha May 26 '17 at 4:37
  • 2
    Ah ok. I only have 10.3. Maybe time to upgrade... – Ian Miller May 26 '17 at 4:38

PHP, 28 Bytes

for(;a&$argn[$i++];)echo" ";

Try it online!

PHP, 29 Bytes

<?=str_pad('',strlen($argn));

Try it online!

C, 31 bytes

f(char*c){puts(""),*c++&&f(c);}
  • 1
    How does this differ from your other C answer? Clearly this one is shorter, but should you have simply edited the other one? Should it just be one answer with two solutions? – Tas May 26 '17 at 1:09
  • 4
    @Tas First of all, I guess in some sense I feel this is not as good as the other one eventhough it's shorter, because it doesn't actually compile as-is. It's just a function so you need to write some main routine around it. However, it is shorter and others seems to post just functions. Clearly it's two very different solutions. One is not the refinement of the other, so to me it makes sense that it should be two different answers. However, I'm new to this community. Is the consensus that one user only posts one answer? If so I will do that next time. – sigvaldm May 26 '17 at 8:24
  • Should the comma really be a comma and not a semicolon? – Oskar Skog May 27 '17 at 11:12
  • 1
    @OskarSkog well, in this case it doesn't matter that much because there is no lhs – cat May 28 '17 at 11:23
  • 1
    @OskarSkog Yes, it should be a comma. As @cat says, it doesn't really matter in this case but I chose comma for variation :) The comma operator evaluates two expressions (e.g. i++, j++ in a for loop) and returns the rightmost one. An important detail is that the recursion has to stop somehow. && doesn't evaluate it's rhs if it's lhs is false. *c++ evaluates false when it points to the null-termination of the string. – sigvaldm May 29 '17 at 18:55

Brainfuck, 16 bytes

Prints newlines.

,[>++++++++++.,]

Excel VBA, 17 15 Bytes

Anonymous VBE immediate window funtion that takes input from cell [A1] and outputs spaces of length of the input to the VBE immediate window

?Spc([Len(A1)])

JavaScript ES6, 22 bytes

a=>a.replace(/./g," ")

f=a=>a.replace(/./g," ");

var test = f("Hello, World!");
console.log(test, test.length);

  • 3
    Huh, I thought "oh darn, it'd have to be s=>s.replace(/[^]/g," "), a byte longer than the other solution". It didn't occur to me that newlines are allowed in the output :P – ETHproductions May 25 '17 at 15:05

Octave, 14 bytes

@(a)["" 0*a+32]

CJam, 4 bytes

q,S*

Try it online!

Explanation

q     e# Read input
 ,    e# Length
  S*  e# Repeat space that many times

C, 45 bytes

Using main. Compile with gcc, ignore warnings.

main(c,v)char**v;{while(*(v[1]++))puts("");}

Usage:

$./a.out "Hello, World!"
  • 1
    Any reason why you can't put char**v in main(c,v)? – CalculatorFeline May 26 '17 at 3:07
  • @CalculatorFeline At least GCC 6.3.1 compiling simply with gcc main.c doesn't seem to allow mixing ANSI function definition with K&R function definition, so main(c,char**v) won't compile. I either have to do main(int c,char**v) or main(c,v)char**v; of which the latter is 3 bytes shorter. You wouldn't by chance know any flag or something which allows mixing these styles? – sigvaldm May 26 '17 at 8:12
  • 3
    No, you can't mix 'em. There's no flag that allows that. K&R style is long obsolete, used only for code golfing and obfuscation purposes. – Cody Gray May 26 '17 at 11:50
  • And I'm guessing removing char**v entirely doesn't compile either. – CalculatorFeline May 26 '17 at 20:39
  • @CalculatorFeline If you omit char** entirely the compiler will interpret it as int. If I'm not mistaken you get an error trying to dereference an int and even if you didn't the program wouldn't do what you expected it to do since an int consumes several chars and therefore you never get a NULL value. – sigvaldm May 29 '17 at 19:04

05AB1E, 3 bytes

vð?

Try it online!

v   # For each character...
 ð? #    Output a space without a newline

Other 3 byte solutions (Thanks Magic Octopus Urn for some of these)

v¶? # For each character print a newline (without adding a newline)
võ, # For each character print the empty string with a newline
gð× # Get the length, concatenate that many copies of space
g¶× # Get the length, concatenate that many copies of newline
Sð: # Split, push a space, replace each char in input with a space
ðs∍ # Push ' ', swap, make the string of spaces as long as the input was

JavaScript (ES6), 23 bytes

s=>" ".repeat(s.length)

Excel, 18 bytes

=REPT(" ",LEN(A1))

Pretty boring and one byte longer than the VBA answer.

><>, 7 bytes

i0(?;ao

The program is a loop

i         //Push a character from the input onto the stack
 0        //Add a 0 to the stack
  (       //Pop the top two values of the stack, and push a 1 if the second is less than the first (In this case, the input has all been read), else push a 0
   ?      //Pop the top of the stack. If the value is a 0, skip the next instruction
    ;     // Terminate the program
     a    // Add a newline to the stack
      o   // Pop the top character of the stack and print it

V, 2 bytes

Ò 

Try it online!

Note the trailing space!

Hexagony, 12 11 bytes

-1 byte thanks to Martin Ender

,<.;.M@.>~8

Try it online!

Here is the expanded hex:

  , < . 
 ; . M @
. > ~ 8 .
 . . . .
  . . .

While there is input, this code runs:

,        # Get input
 <       # Turn right (SE) if we didn't get EOF
  M8     # Set the memory edge to 778 which is 10 (mod 256)
    ;    # Print as a character (newline)
     >   # Redirect East
      ~  # Multiply by -1. This makes the pointer go to the top when it runs off the edge
       8 # Effectively a no-op.

When EOF is reached:

,    # Get input
 <   # Turn left (NE)
  8  # Effectively a no-op
   @ # End program
  • You can print a linefeed in three bytes with M8; (which gives 778 = 10 (mod 256)). That should allow you to move the ~ where the ; is right now, saving a byte. – Martin Ender Jun 7 '17 at 9:52

Perl 5, 7 bytes

-1 byte thanks to @Xcali

6 bytes of code + -p flag.

y// /c

Try it online!

Quite straight forward : replaces every character with a space.

  • 1
    y// /c is one byte shorter. – Xcali Oct 26 '17 at 18:49

Python 2, 25 bytes

exec'print;'*len(input())

-2 bytes thanks to Loovjo
-2 bytes in the invalid code thanks to totallyhuman :p
-3 bytes

  • 1
    You can remove the parens after exec since it's a keyword in Python 2 – Loovjo May 25 '17 at 13:36
  • 1
    @Loovjo Oh right, Python 2. Thanks! – HyperNeutrino May 25 '17 at 13:47
  • I know this is old and stuff but exec'print;'*len(input()) works. – totallyhuman Jul 20 '17 at 14:28
  • 1
    @totallyhuman oh true, thanks :P – HyperNeutrino Jul 20 '17 at 15:15
  • 1
    @TheMatt it's probably not in the problem specs but it's one of the default acceptable input methods. Try looking on meta, I don't want to go looking for it right now – HyperNeutrino Jul 13 at 16:23

PHP, 36 bytes

<?=str_repeat('
',strlen($argv[1]));

Try it online!

Outputs newlines because spaces are too mainstream

  • $argn instead of $argv[1] save 4 bytes. Run with the -F option – Jörg Hülsermann Jun 15 '17 at 16:23

Cubix, 6 bytes

Wahoo a 6 byter!

wi?@oS

Cubified

  w
i ? @ o
  S
  • i gets input
  • ? test top of stack
    • if negative (EOI) redirect onto w lane change which umps to the @ halt
    • if 0 (null) halt this shouldn't be hit
    • if positive Sow push space to the stack, output and change lane onto i

Try it online!

  • Sweet, it's not too often a Cubix program is this short :-) – ETHproductions May 27 '17 at 15:40

C, 32 bytes

Try Online modifying characters into spaces

f(char*t){(*t=*t?32:0)&&f(t+1);}

C, 37 bytes

Try Online Left-padding the end-of-string with its length

f(char*t){printf("%*c",strlen(t),0);}

APL (Dyalog) 13.2, 1 byte

Prints only spaces.

 prototype (numbers become zeros, characters become spaces)

Try it online!

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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