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 code-golf so fewest bytes wins!
146 Answers 146
Whitespace, (削除) 311 (削除ここまで) (削除) 150 (削除ここまで) (削除) 77 (削除ここまで) (削除) 68 (削除ここまで) (削除) 65 (削除ここまで) (削除) 46 (削除ここまで) (削除) 41 (削除ここまで) 38 bytes
-3 bytes thanks to Kevin Cruijssen
-27 bytes thanks to Ephphatha
  
       
 
  
 
 
  
  
 
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)
-
29\$\begingroup\$ Assuming this does actually work, this definitely wins my vote for most creative answer \$\endgroup\$Mayube– Mayube2017年05月25日 14:25:54 +00:00Commented May 25, 2017 at 14:25
-
25\$\begingroup\$ Wait where is the answer? Is it invisible too? \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年05月25日 14:27:01 +00:00Commented May 25, 2017 at 14:27
-
18\$\begingroup\$ WHAT BLACK MAGIC IS THIS. Your code is not even there! -1 \$\endgroup\$user63187– user631872017年05月25日 14:32:07 +00:00Commented May 25, 2017 at 14:32
-
31\$\begingroup\$ @Christopher more like WHITEspace MAGIC \$\endgroup\$Rod– Rod2017年05月25日 14:34:58 +00:00Commented May 25, 2017 at 14:34
-
15\$\begingroup\$ I knew someone would answer this question with a whitespace program \$\endgroup\$Draco18s no longer trusts SE– Draco18s no longer trusts SE2017年05月25日 21:06:57 +00:00Commented May 25, 2017 at 21:06
-
23\$\begingroup\$ Does japt seriously have a builtin for this? Damn... \$\endgroup\$Mayube– Mayube2017年05月25日 13:06:28 +00:00Commented 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\$Tom– Tom2017年05月25日 13:12:09 +00:00Commented May 25, 2017 at 13:12
-
4
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.
-
21\$\begingroup\$ Its like an adorable little finless fish \$\endgroup\$Taylor Raine– Taylor Raine2017年05月25日 13:27:31 +00:00Commented May 25, 2017 at 13:27
-
\$\begingroup\$ dang this blows my haskell answer out of the water haha \$\endgroup\$Connor D– Connor D2020年09月23日 02:24:50 +00:00Commented Sep 23, 2020 at 2:24
brainfuck, 18 bytes
++++++++++>,[<.>,]
Prints one newline for each byte of input. Printing spaces instead would add 4 bytes.
-
5\$\begingroup\$ For posterity: Mika Lammi posted a clever 16-byte answer that got buried.
,[>++++++++++.,]\$\endgroup\$lynn– lynn2018年06月23日 14:03:17 +00:00Commented Jun 23, 2018 at 14:03
Python, 19 bytes
lambda s:' '*len(s)
Brainfuck, 16 bytes
Prints newlines.
,[>++++++++++.,]
Retina, (削除) 3 (削除ここまで) 4 bytes
S\`.
Old version, doesn't work because Retina prints a trailing line feed.
.  
(The second line contains a space).
-
2\$\begingroup\$ The retina TIO is quite easy to use. Here is your answer \$\endgroup\$Digital Trauma– Digital Trauma2017年05月25日 17:44:52 +00:00Commented 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 useS\`.though, which replaces each character with a linefeed (because it splits the input around each character). \$\endgroup\$Martin Ender– Martin Ender2017年06月07日 09:54:13 +00:00Commented 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\$TheLethalCoder– TheLethalCoder2017年06月07日 09:57:24 +00:00Commented Jun 7, 2017 at 9:57
C#, (削除) 28 (削除ここまで) 24 bytes
s=>"".PadLeft(s.Length);
Old version using the string constructor for 28 bytes:
s=>new string(' ',s.Length);
-
3\$\begingroup\$ Wanted to do exactly the same \$\endgroup\$LiefdeWen– LiefdeWen2017年05月25日 12:59:31 +00:00Commented 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\$TheLethalCoder– TheLethalCoder2017年05月25日 13:00:49 +00:00Commented May 25, 2017 at 13:00
-
\$\begingroup\$ Given adequate type context, couldn't this just be
s=>new(' ',s.Length)? \$\endgroup\$Bbrk24– Bbrk242023年03月15日 15:06:34 +00:00Commented Mar 15, 2023 at 15:06
JavaScript ES6, 22 bytes
a=>a.replace(/./g," ")
f=a=>a.replace(/./g," ");
var test = f("Hello, World!");
console.log(test, test.length);
-
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\$ETHproductions– ETHproductions2017年05月25日 15:05:20 +00:00Commented May 25, 2017 at 15:05
Retina, 5 bytes
\`.
¶
Try it online! Changes everything into newlines. The \` suppresses the extra newline Retina would normally output.
Mathematica, 21 bytes
StringReplace[_->" "]
-
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\$CalculatorFeline– CalculatorFeline2017年05月26日 03:03:52 +00:00Commented May 26, 2017 at 3:03 -
1\$\begingroup\$ Doesn't this need a
#and a&in it? E.g.StringReplace[#,_->" "]&\$\endgroup\$Ian Miller– Ian Miller2017年05月26日 03:58:53 +00:00Commented May 26, 2017 at 3:58 -
3\$\begingroup\$ @IanMiller Not in Mathematica 10.4 or 11. reference.wolfram.com/language/ref/StringReplace.html \$\endgroup\$alephalpha– alephalpha2017年05月26日 04:37:58 +00:00Commented May 26, 2017 at 4:37
-
2\$\begingroup\$ Ah ok. I only have 10.3. Maybe time to upgrade... \$\endgroup\$Ian Miller– Ian Miller2017年05月26日 04:38:41 +00:00Commented May 26, 2017 at 4:38
C, 31 bytes
f(char*c){puts(""),*c++&&f(c);}
-
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\$Tas– Tas2017年05月26日 01:09:23 +00:00Commented 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\$sigvaldm– sigvaldm2017年05月26日 08:24:41 +00:00Commented May 26, 2017 at 8:24
-
\$\begingroup\$ Should the comma really be a comma and not a semicolon? \$\endgroup\$Oskar Skog– Oskar Skog2017年05月27日 11:12:55 +00:00Commented 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\$cat– cat2017年05月28日 11:23:57 +00:00Commented 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\$sigvaldm– sigvaldm2017年05月29日 18:55:35 +00:00Commented May 29, 2017 at 18:55
PHP, 28 Bytes
for(;a&$argn[$i++];)echo" ";
PHP, 29 Bytes
<?=str_pad('',strlen($argn));
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)])
05AB1E, 3 bytes
vð?
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)
-
1\$\begingroup\$ Other solution:
gð×, the rest I came up with were above 3 like:õ‚.B¤\$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2017年05月25日 17:16:38 +00:00Commented May 25, 2017 at 17:16 -
2\$\begingroup\$ Another fun one:
Sð:\$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2017年10月26日 14:29:52 +00:00Commented Oct 26, 2017 at 14:29 -
1\$\begingroup\$ More fun:
ðs∍\$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2018年10月22日 16:33:06 +00:00Commented 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\$Kevin Cruijssen– Kevin Cruijssen2019年11月15日 13:33:32 +00:00Commented Nov 15, 2019 at 13:33
JavaScript (ES6), 23 bytes
s=>" ".repeat(s.length)
Octave, 14 bytes
@(a)["" 0*a+32]
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\$\begingroup\$ Any reason why you can't put
char**vinmain(c,v)? \$\endgroup\$CalculatorFeline– CalculatorFeline2017年05月26日 03:07:15 +00:00Commented May 26, 2017 at 3:07 -
\$\begingroup\$ @CalculatorFeline At least GCC 6.3.1 compiling simply with
gcc main.cdoesn't seem to allow mixing ANSI function definition with K&R function definition, somain(c,char**v)won't compile. I either have to domain(int c,char**v)ormain(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\$sigvaldm– sigvaldm2017年05月26日 08:12:59 +00:00Commented 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\$Cody Gray– Cody Gray2017年05月26日 11:50:28 +00:00Commented May 26, 2017 at 11:50
-
\$\begingroup\$ And I'm guessing removing
char**ventirely doesn't compile either. \$\endgroup\$CalculatorFeline– CalculatorFeline2017年05月26日 20:39:54 +00:00Commented May 26, 2017 at 20:39 -
\$\begingroup\$ @CalculatorFeline If you omit
char**entirely the compiler will interpret it asint. If I'm not mistaken you get an error trying to dereference anintand even if you didn't the program wouldn't do what you expected it to do since anintconsumes severalchars and therefore you never get aNULLvalue. \$\endgroup\$sigvaldm– sigvaldm2017年05月29日 19:04:37 +00:00Commented May 29, 2017 at 19:04
><>, 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
Hexagony, (削除) 12 (削除ここまで) 11 bytes
-1 byte thanks to Martin Ender
,<.;.M@.>~8
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
-
\$\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\$Martin Ender– Martin Ender2017年06月07日 09:52:28 +00:00Commented Jun 7, 2017 at 9:52
Pyth, 3 bytes
*dl
Python equivalent: len(input())*" "
jmk
Python equivalent: "\n".join(map("", input()))
smb
Python equivalent: "".join(map("\n",input())
VQk
Python equivalent: For N in input():print("")
-
\$\begingroup\$ @StanStrum: It outputs newlines. Newlines are also allowed per the challenge description above. You can see this better from this example input \$\endgroup\$KarlKastor– KarlKastor2017年09月18日 19:41:38 +00:00Commented Sep 18, 2017 at 19:41
Perl 5, 7 bytes
-1 byte thanks to @Xcali
6 bytes of code + -p flag.
y// /c
Quite straight forward : replaces every character with a space.
-
1\$\begingroup\$
y// /cis one byte shorter. \$\endgroup\$Xcali– Xcali2017年10月26日 18:49:57 +00:00Commented Oct 26, 2017 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\$\begingroup\$ You can remove the parens after
execsince it's a keyword in Python 2 \$\endgroup\$xenia– xenia2017年05月25日 13:36:06 +00:00Commented May 25, 2017 at 13:36 -
1\$\begingroup\$ @Loovjo Oh right, Python 2. Thanks! \$\endgroup\$2017年05月25日 13:47:13 +00:00Commented May 25, 2017 at 13:47
-
\$\begingroup\$ I know this is old and stuff but
exec'print;'*len(input())works. \$\endgroup\$totallyhuman– totallyhuman2017年07月20日 14:28:51 +00:00Commented Jul 20, 2017 at 14:28 -
\$\begingroup\$ Oh, my bad, didn't think of that. \$\endgroup\$totallyhuman– totallyhuman2017年07月20日 15:16:40 +00:00Commented 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\$2018年07月13日 16:23:15 +00:00Commented Jul 13, 2018 at 16:23
PHP, 36 bytes
<?=str_repeat('
',strlen($argv[1]));
Outputs newlines because spaces are too mainstream
-
\$\begingroup\$
$argninstead of$argv[1]save 4 bytes. Run with the-Foption \$\endgroup\$Jörg Hülsermann– Jörg Hülsermann2017年06月15日 16:23:26 +00:00Commented Jun 15, 2017 at 16:23
0x0Aand0x20are the hexadecimal values for the Newline and Space characters respectively \$\endgroup\$These can be any mix of spaces and newlinesYour 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\$