45
\$\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\$
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\$
    – manatwork
    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\$
    – Mayube
    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\$
    – manatwork
    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\$
    – Mayube
    Commented May 25, 2017 at 13:05
  • 1
    \$\begingroup\$ Can we assume the input will only have printable characters? \$\endgroup\$
    – Luis Mendo
    Commented May 25, 2017 at 13:30

138 Answers 138

1
2 3 4 5
153
\$\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)
\$\endgroup\$
17
  • 28
    \$\begingroup\$ Assuming this does actually work, this definitely wins my vote for most creative answer \$\endgroup\$
    – Mayube
    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\$
    – user63187
    Commented May 25, 2017 at 14:32
  • 31
    \$\begingroup\$ @Christopher more like WHITEspace MAGIC \$\endgroup\$
    – Rod
    Commented May 25, 2017 at 14:34
  • 14
    \$\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!

\$\endgroup\$
3
  • 23
    \$\begingroup\$ Does japt seriously have a builtin for this? Damn... \$\endgroup\$
    – Mayube
    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\$
    – Tom
    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\$
    – Oliver
    Commented May 25, 2017 at 13:57
40
\$\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.

\$\endgroup\$
2
  • 20
    \$\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\$
    – Connor D
    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.

\$\endgroup\$
1
  • 5
    \$\begingroup\$ For posterity: Mika Lammi posted a clever 16-byte answer that got buried. ,[>++++++++++.,] \$\endgroup\$
    – lynn
    Commented Jun 23, 2018 at 14:03
20
\$\begingroup\$

Python, 19 bytes

lambda s:' '*len(s)
\$\endgroup\$
18
\$\begingroup\$

Brainfuck, 16 bytes

Prints newlines.

,[>++++++++++.,]
\$\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).

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

\$\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);
\$\endgroup\$
3
  • 3
    \$\begingroup\$ Wanted to do exactly the same \$\endgroup\$
    – LiefdeWen
    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\$
    – Bbrk24
    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);

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

\$\endgroup\$
8
\$\begingroup\$

Mathematica, 21 bytes

StringReplace[_->" "]
\$\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\$
    – Ian Miller
    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\$
    – alephalpha
    Commented May 26, 2017 at 4:37
  • 2
    \$\begingroup\$ Ah ok. I only have 10.3. Maybe time to upgrade... \$\endgroup\$
    – Ian Miller
    Commented May 26, 2017 at 4:38
8
\$\begingroup\$

C, 31 bytes

f(char*c){puts(""),*c++&&f(c);}
\$\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\$
    – Tas
    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\$
    – sigvaldm
    Commented May 26, 2017 at 8:24
  • \$\begingroup\$ Should the comma really be a comma and not a semicolon? \$\endgroup\$
    – Oskar Skog
    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\$
    – cat
    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\$
    – sigvaldm
    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!

\$\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)])
\$\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)
\$\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)
\$\endgroup\$
6
\$\begingroup\$

Octave, 14 bytes

@(a)["" 0*a+32]
\$\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
\$\endgroup\$
6
\$\begingroup\$

V, 2 bytes

Ò 

Try it online!

Note the trailing space!

\$\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!"
\$\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\$
    – sigvaldm
    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\$
    – Cody Gray
    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\$
    – sigvaldm
    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.

\$\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
\$\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
\$\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("")

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

\$\endgroup\$
1
  • 1
    \$\begingroup\$ y// /c is one byte shorter. \$\endgroup\$
    – Xcali
    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

\$\endgroup\$
9
  • 1
    \$\begingroup\$ You can remove the parens after exec since it's a keyword in Python 2 \$\endgroup\$
    – xenia
    Commented May 25, 2017 at 13:36
  • 1
    \$\begingroup\$ @Loovjo Oh right, Python 2. Thanks! \$\endgroup\$
    – hyperneutrino
    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
  • 1
    \$\begingroup\$ @totallyhuman oh true, thanks :P \$\endgroup\$
    – hyperneutrino
    Commented Jul 20, 2017 at 15:15
  • 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\$
    – hyperneutrino
    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

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

⁶ṁ

Try it online!

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

\$\endgroup\$
1
2 3 4 5

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.