47
\$\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!

asked May 25, 2017 at 12:50
\$\endgroup\$
13
  • 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\$ Commented May 25, 2017 at 12:56
  • 1
    \$\begingroup\$ @manatwork 0x0A and 0x20 are the hexadecimal values for the Newline and Space characters respectively \$\endgroup\$ Commented May 25, 2017 at 12:58
  • 1
    \$\begingroup\$ "output a number of whitespace characters (0x0A and 0x20)" – Where in the output should those newline characters be? \$\endgroup\$ Commented May 25, 2017 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\$ Commented May 25, 2017 at 13:05
  • 1
    \$\begingroup\$ Can we assume the input will only have printable characters? \$\endgroup\$ Commented May 25, 2017 at 13:30

146 Answers 146

1
2 3 4 5
157
\$\begingroup\$

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)
answered May 25, 2017 at 14:24
\$\endgroup\$
17
  • 29
    \$\begingroup\$ Assuming this does actually work, this definitely wins my vote for most creative answer \$\endgroup\$ Commented May 25, 2017 at 14:25
  • 25
    \$\begingroup\$ Wait where is the answer? Is it invisible too? \$\endgroup\$ Commented May 25, 2017 at 14:27
  • 18
    \$\begingroup\$ WHAT BLACK MAGIC IS THIS. Your code is not even there! -1 \$\endgroup\$ Commented May 25, 2017 at 14:32
  • 31
    \$\begingroup\$ @Christopher more like WHITEspace MAGIC \$\endgroup\$ Commented May 25, 2017 at 14:34
  • 15
    \$\begingroup\$ I knew someone would answer this question with a whitespace program \$\endgroup\$ Commented May 25, 2017 at 21:06
61
\$\begingroup\$

Japt, 1 byte

ç

Try it online!

answered May 25, 2017 at 12:59
\$\endgroup\$
3
  • 23
    \$\begingroup\$ Does japt seriously have a builtin for this? Damn... \$\endgroup\$ Commented May 25, 2017 at 13:06
  • 25
    \$\begingroup\$ @Mayube well it has a builtin to replace all characters in a string with another, and the default replacement is a space ;) \$\endgroup\$ Commented May 25, 2017 at 13:12
  • 4
    \$\begingroup\$ Very nice! For those running the program, you can add the -Q flag into the input to put quotes around the output. TIO \$\endgroup\$ Commented May 25, 2017 at 13:57
41
\$\begingroup\$

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.

answered May 25, 2017 at 12:57
\$\endgroup\$
2
  • 21
    \$\begingroup\$ Its like an adorable little finless fish \$\endgroup\$ Commented May 25, 2017 at 13:27
  • \$\begingroup\$ dang this blows my haskell answer out of the water haha \$\endgroup\$ Commented Sep 23, 2020 at 2:24
21
\$\begingroup\$

brainfuck, 18 bytes

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

Try it online!

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

answered May 25, 2017 at 13:05
\$\endgroup\$
1
  • 5
    \$\begingroup\$ For posterity: Mika Lammi posted a clever 16-byte answer that got buried. ,[>++++++++++.,] \$\endgroup\$ Commented Jun 23, 2018 at 14:03
21
\$\begingroup\$

Python, 19 bytes

lambda s:' '*len(s)
Taylor Raine
8,9732 gold badges29 silver badges53 bronze badges
answered May 25, 2017 at 12:59
\$\endgroup\$
21
\$\begingroup\$

Brainfuck, 16 bytes

Prints newlines.

,[>++++++++++.,]
answered May 26, 2017 at 12:30
\$\endgroup\$
17
\$\begingroup\$

Retina, (削除) 3 (削除ここまで) 4 bytes

S\`.

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

.
 

(The second line contains a space).

answered May 25, 2017 at 13:35
\$\endgroup\$
3
  • 2
    \$\begingroup\$ The retina TIO is quite easy to use. Here is your answer \$\endgroup\$ Commented May 25, 2017 at 17:44
  • 2
    \$\begingroup\$ 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). \$\endgroup\$ Commented Jun 7, 2017 at 9:54
  • \$\begingroup\$ @MartinEnder Ahhh wasn't sure if that was a Retina or TIO thing. Thanks for the help on saving a byte there though! \$\endgroup\$ Commented Jun 7, 2017 at 9:57
14
\$\begingroup\$

sed, 7 bytes

s/./ /g

Try it online!

answered May 25, 2017 at 21:47
\$\endgroup\$
12
\$\begingroup\$

C#, (削除) 28 (削除ここまで) 24 bytes

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

Old version using the string constructor for 28 bytes:

s=>new string(' ',s.Length);
answered May 25, 2017 at 12:54
\$\endgroup\$
3
  • 3
    \$\begingroup\$ Wanted to do exactly the same \$\endgroup\$ Commented May 25, 2017 at 12:59
  • 1
    \$\begingroup\$ @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... \$\endgroup\$ Commented May 25, 2017 at 13:00
  • \$\begingroup\$ Given adequate type context, couldn't this just be s=>new(' ',s.Length)? \$\endgroup\$ Commented Mar 15, 2023 at 15:06
9
\$\begingroup\$

JavaScript ES6, 22 bytes

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

f=a=>a.replace(/./g," ");
var test = f("Hello, World!");
console.log(test, test.length);

answered May 25, 2017 at 12:55
\$\endgroup\$
1
  • 3
    \$\begingroup\$ 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 \$\endgroup\$ Commented May 25, 2017 at 15:05
9
\$\begingroup\$

Retina, 5 bytes

\`.
¶

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

answered May 25, 2017 at 13:22
\$\endgroup\$
8
\$\begingroup\$

Mathematica, 21 bytes

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

C, 31 bytes

f(char*c){puts(""),*c++&&f(c);}
answered May 25, 2017 at 16:12
\$\endgroup\$
5
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ Commented May 26, 2017 at 1:09
  • 4
    \$\begingroup\$ @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. \$\endgroup\$ Commented May 26, 2017 at 8:24
  • \$\begingroup\$ Should the comma really be a comma and not a semicolon? \$\endgroup\$ Commented May 27, 2017 at 11:12
  • 1
    \$\begingroup\$ @OskarSkog well, in this case it doesn't matter that much because there is no lhs \$\endgroup\$ Commented May 28, 2017 at 11:23
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ Commented May 29, 2017 at 18:55
7
\$\begingroup\$

PHP, 28 Bytes

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

Try it online!

PHP, 29 Bytes

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

Try it online!

answered May 25, 2017 at 13:24
\$\endgroup\$
0
7
\$\begingroup\$

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)])
answered May 25, 2017 at 13:22
\$\endgroup\$
0
7
\$\begingroup\$

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 and Kevin Cruijssen for most 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
võJ # For each char, push a space and ''.join(stack)
v¶J # For each char, push a newline and ''.join(stack)
€ðJ # For each char, push a space. Then ''.join(stack)
€¶J # For each char, push a newline. Then ''.join(stack)
answered May 25, 2017 at 13:20
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Other solution: gð×, the rest I came up with were above 3 like: õ‚.B¤ \$\endgroup\$ Commented May 25, 2017 at 17:16
  • 2
    \$\begingroup\$ Another fun one: Sð: \$\endgroup\$ Commented Oct 26, 2017 at 14:29
  • 1
    \$\begingroup\$ More fun: ðs∍ \$\endgroup\$ Commented Oct 22, 2018 at 16:33
  • \$\begingroup\$ Some more alternative 3-byters: võJ/v¶J; €ðJ/€¶J. And since a sequence of characters as I/O is allowed by default when strings I/O is asked, some 2-byte versions are possible: €ð/€¶/εð/ε¶ and ð:/¶:. Although since this is a pretty old challenge and all other answers use actual strings, I could understand if you kept it as string I/O. \$\endgroup\$ Commented Nov 15, 2019 at 13:33
6
\$\begingroup\$

JavaScript (ES6), 23 bytes

s=>" ".repeat(s.length)
answered May 25, 2017 at 12:57
\$\endgroup\$
6
\$\begingroup\$

Octave, 14 bytes

@(a)["" 0*a+32]
answered May 25, 2017 at 13:17
\$\endgroup\$
6
\$\begingroup\$

CJam, 4 bytes

q,S*

Try it online!

Explanation

q e# Read input
 , e# Length
 S* e# Repeat space that many times
answered May 25, 2017 at 13:30
\$\endgroup\$
6
\$\begingroup\$

V, 2 bytes

Ò 

Try it online!

Note the trailing space!

Erik the Outgolfer
40.8k5 gold badges46 silver badges125 bronze badges
answered May 25, 2017 at 15:38
\$\endgroup\$
6
\$\begingroup\$

C, 45 bytes

Using main. Compile with gcc, ignore warnings.

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

Usage:

$./a.out "Hello, World!"
Taylor Raine
8,9732 gold badges29 silver badges53 bronze badges
answered May 25, 2017 at 15:54
\$\endgroup\$
8
  • 1
    \$\begingroup\$ Any reason why you can't put char**v in main(c,v)? \$\endgroup\$ Commented May 26, 2017 at 3:07
  • \$\begingroup\$ @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? \$\endgroup\$ Commented May 26, 2017 at 8:12
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ Commented May 26, 2017 at 11:50
  • \$\begingroup\$ And I'm guessing removing char**v entirely doesn't compile either. \$\endgroup\$ Commented May 26, 2017 at 20:39
  • \$\begingroup\$ @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. \$\endgroup\$ Commented May 29, 2017 at 19:04
5
\$\begingroup\$

Excel, 18 bytes

=REPT(" ",LEN(A1))

Pretty boring and one byte longer than the VBA answer.

answered May 25, 2017 at 13:29
\$\endgroup\$
5
\$\begingroup\$

><>, 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
answered May 25, 2017 at 14:38
\$\endgroup\$
5
\$\begingroup\$

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
answered May 25, 2017 at 22:09
\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jun 7, 2017 at 9:52
5
\$\begingroup\$

Pyth, 3 bytes

*dl

Try it!

Python equivalent: len(input())*" "

jmk

Try this!

Python equivalent: "\n".join(map("", input()))

smb

Try that!

Python equivalent: "".join(map("\n",input())

VQk

Try!

Python equivalent: For N in input():print("")

answered May 25, 2017 at 20:06
\$\endgroup\$
1
  • \$\begingroup\$ @StanStrum: It outputs newlines. Newlines are also allowed per the challenge description above. You can see this better from this example input \$\endgroup\$ Commented Sep 18, 2017 at 19:41
5
\$\begingroup\$

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.

answered May 25, 2017 at 13:24
\$\endgroup\$
1
  • 1
    \$\begingroup\$ y// /c is one byte shorter. \$\endgroup\$ Commented Oct 26, 2017 at 18:49
5
\$\begingroup\$

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

answered May 25, 2017 at 13:14
\$\endgroup\$
8
  • 1
    \$\begingroup\$ You can remove the parens after exec since it's a keyword in Python 2 \$\endgroup\$ Commented May 25, 2017 at 13:36
  • 1
    \$\begingroup\$ @Loovjo Oh right, Python 2. Thanks! \$\endgroup\$ Commented May 25, 2017 at 13:47
  • \$\begingroup\$ I know this is old and stuff but exec'print;'*len(input()) works. \$\endgroup\$ Commented Jul 20, 2017 at 14:28
  • \$\begingroup\$ Oh, my bad, didn't think of that. \$\endgroup\$ Commented Jul 20, 2017 at 15:16
  • 1
    \$\begingroup\$ @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 \$\endgroup\$ Commented Jul 13, 2018 at 16:23
4
\$\begingroup\$

PHP, 36 bytes

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

Try it online!

Outputs newlines because spaces are too mainstream

answered May 25, 2017 at 13:09
\$\endgroup\$
1
  • \$\begingroup\$ $argn instead of $argv[1] save 4 bytes. Run with the -F option \$\endgroup\$ Commented Jun 15, 2017 at 16:23
4
\$\begingroup\$

Jelly, 2 bytes

6ṁ

Try it online!

answered May 25, 2017 at 14:00
\$\endgroup\$
4
\$\begingroup\$

C (tcc), 31 bytes

I opted to output newlines since it's shorter...

f(char*s){for(;*s++;puts(""));}

Try it online!

answered May 25, 2017 at 15:00
\$\endgroup\$
1
2 3 4 5

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.