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

1
\$\begingroup\$

Flurry -bnb, 42 bytes

[]{(){}{}}[]{({})}<((({<({}){}>})){}){}{}>

Try it online!

Run example (using Haskell interpreter)

$ printf "Hello World!" | ./flurry -bnb a.flr
          
$ printf "Hello World!" | ./flurry -bnb a.flr | wc -c
12

It takes 24 bytes just to construct a single space (32): <((({<({}){}>})){}){}{}>. Even worse is that it takes 2 more byets to construct a single newline (10): (<><<>()>)<(<({}){}>{}){}>...

The algorithm is: empty the stack, preserving the value of stack height, and then push space that many times.

main = height pop-I height push-I 32

// Return self, popping and discarding one item from stack
pop-I = \x. K x pop
// (height pop-I height) returns original stack height with
// the stack emptied as a side effect

// Return self, pushing itself to the stack once
push-I = \x. push x

32 = 2**2 * 2**2 * 2
= <(2 2)(2 2)2>
= <(push ((push (push 2)) pop)) pop pop>
\$\endgroup\$
1
\$\begingroup\$

Rockstar, 27 bytes

listen to S
cut S
say " "*S

Try it here (Code will need to be pasted in)

Replace the last line with the below to wrap the output in single quotes.

say "'"+" "*S+"'"
\$\endgroup\$
1
\$\begingroup\$

Husk, 4 bytes

R' L

Try it online! (Note: prints xs instead of spaces so that the output is visible; change the x to to [not] see the real version)

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

Nim, 38 bytes

for _ in stdin.readAll:stdout.write" "

Try it online!

Since Nim is style-insensitive, something like this also works:

fOr _ iN sTdIn.rEaDaLl:sTdOuT.wRiTe" "

Try it online!

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

05AB1E, 3 bytes

gð×

Try it online!

gð×  # full program
 ð   # a space...
  ×  # repeated...
g    # length of input...
  ×  # times
\$\endgroup\$
1
  • \$\begingroup\$ ...nice, but quite similar to this... \$\endgroup\$ Nov 17 '20 at 18:06
0
\$\begingroup\$

REXX 27 Bytes

say left("",length(arg(1)))
\$\endgroup\$
0
\$\begingroup\$

ZX80 (4K ROM version with sanity check)

~58 bytes (listing)

 1 INPUT A$
 2 IF A$="" THEN GO TO 1
 3 PRINT " ";
 4 LET A$=TL$(A$)
 5 IF A$="" THEN STOP
 6 GO TO 3

Line 2 can be removed to save RAMs. However, if an empty string is entered without line 2 then it will PRINT one space.

\$\endgroup\$
1
  • \$\begingroup\$ It is now later. \$\endgroup\$ May 26 '17 at 3:08
0
\$\begingroup\$

TXR Lisp, 22 19 bytes:

(op regsub #/./" ")

Previously:

(op mapcar(ret[" "0]))

That is a function to which we can pass a string:

REPL:

1> (op regsub #/./" ")
#<interpreted fun: lambda #:rest-0164>
2> [*1 "abc"]
"   "

"   "
\$\endgroup\$
0
\$\begingroup\$

Clojurescript, 27 bytes

#(apply str(map(fn[]" ")%))

Because it's based on js, clojurescript doesn't care about arity errors. That saves one byte over the clojure eqivalent.

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

Windows batch, 115 bytes

@set i=%~1
@set p=0
@set/ac=-1
:N
@call set t=%%i:~%p%,1%%
@set/ac+=1
@set/ap+=1
@if "%t%" NEQ "" @goto N
@echo %c%

Re-used code from my answer in Is the checkbox not not unchecked?

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

Bash + Coreutils, 11 Bytes

tr -c \ \

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

Powershell, 22 Bytes

' '*(Read-Host).length
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Not sure if Read-Host counts for input but " "*"$args".length is shorter anyway. Also there is not len property. That should be printing nulls. \$\endgroup\$
    – Matt
    May 30 '17 at 12:17
  • \$\begingroup\$ Length property and Len are same \$\endgroup\$ May 30 '17 at 13:32
  • \$\begingroup\$ "hello".Len is null on my system because that is a non-existent property where as "hello".Length returns 5. If that works for you then you have an alias or extent that I, and most, do not have. \$\endgroup\$
    – Matt
    May 30 '17 at 14:14
  • \$\begingroup\$ Ok...That explains it \$\endgroup\$ May 30 '17 at 14:15
0
\$\begingroup\$

Ruby, 12 11+1 = 13 12 bytes

Uses the -p flag. -1 byte from Martin Ender.

gsub /./,$/

Try it online!

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

J, 6 bytes

' '#~#

Try it online!

Explanation

' '#~#
   #~    repeat
' '      spaces
     #   for length of input
\$\endgroup\$
2
  • \$\begingroup\$ 4 bytes ''"0 outputs n newlines. \$\endgroup\$
    – FrownyFrog
    Oct 28 '17 at 1:07
  • \$\begingroup\$ @FrownyFrog You should make your own answer, it's quite different from mine \$\endgroup\$ Oct 28 '17 at 3:51
0
\$\begingroup\$

Common Lisp, 32 bytes

(format t"~va"(length(read))#\ )

Try it online!

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

Carrot, 12 bytes

#^//()/gS" "

Try it online! (append a ^@^v@ after the code to see the spaces bounded between @s)

Explanation

#^            Set the stack-string to be equal to the input
/             Get matches of this regex
 /()/g         any position (not character) in the string (shorter than /\b|\B/ by 3 bytes)
              If the length of the string is 3, this returns a 4-element array
               consisting of empty strings
S" "          Join on spaces, so the example 4-element array will result in 3 spaces

The space can even be replaced with a literal newline or tab for the same bytecount.

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

Befunge-98 (PyFunge), 5 bytes

#@~a,

Try it online!

For every character in the input (#@~), this prints a new line (a,).

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

dc, 18 bytes

?Zd[9P1-d0<r]sr0<r

Try it online!

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

RProgN 2, 3 bytes

L•*

Uncreative solution is uncreative.

Just gets the length of the input, and multiplies , which is predefined to be a space, by it.

Try it online!

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

Ly, 9 bytes

iy[' o,];

Try it online!

Explanation:

iy[' o,];

iy        # push length of input
  [    ]  # while loop
   ' o    # output a space
      ,   # decrement input length
        ; # terminate (avoids implicit output)
\$\endgroup\$
0
\$\begingroup\$

Jq 1.5, 69 68 66 64 bytes

def L:length;$x|if L<1then empty elif L<2then""else L-1|.*" "end

This is longer then usual to compensate for newline jq normally emits and the odd behavior of * with N<2

Sample runs:

$ jq -Mrn --arg x 'Hello, World!' 'def L:length;$x|if L<1then empty elif L<2then""else L-1|.*" "end'

$ jq -Mrn --arg x 'Hello, World!' 'def L:length;$x|if L<1then empty elif L<2then""else L-1|.*" "end' | wc -c
      13
$ echo -n 'def L:length;$x|if L<1then empty elif L<2then""else L-1|.*" "end' | wc -c
      64

Shout out to Jonathan Frech for finding 3 superfluous spaces!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I do not know jq, but as then""else is syntactically valid, it seems like you could also omit the space in " " end. \$\endgroup\$ Sep 19 '17 at 7:07
  • 1
    \$\begingroup\$ As variables probably cannot begin with a digit, the spaces in <1 then and <2 then may also be superfluous. \$\endgroup\$ Sep 19 '17 at 7:40
0
\$\begingroup\$

Vim, 4 bytes

VGr 

(a trailing space)

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

Julia 0.6, 15 bytes

s->" "^endof(s)

Try it online!

^ applied to strings is the repetition operator, and endof gives the last index of the string, which is equal to the length of the string (since Julia indexing is 1-based).

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

Yabasic, 37 bytes

Takes input, and outputs as many new lines as the length of the input

Line Input""s$
For i=1TO Len(s$)?Next

Try it online!

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

C# .NET, 84 bytes

class P{static void Main(string[]a){System.Console.Write("".PadLeft(a[0].Length));}}

Try Online

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

Kotlin, 20 bytes

{" ".repeat(length)}

{" ".repeat           // repeat space
           (length)}  // length of input

Try it online!

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

Forth (gforth), 9 bytes

typewhite

Try it online!

It is a gforth built-in. A string in gforth is commonly represented as addr length, and typewhite consumes addr length and prints length spaces.

\$\endgroup\$
1
  • \$\begingroup\$ forth golfing language confirmed?! \$\endgroup\$
    – Razetime
    Nov 17 '20 at 15:33
0
\$\begingroup\$

Add++, 21 bytes

D,f,@@,bL*
$f>" ">?
o

Try it online!

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

Check, 6 bytes

," "*o

Pass the input by the command-line arguments as a list of code points, i.e. [72,101,108,108,111,44,32,87,111,114,108,100,33].

Explanation:

  • , gets the length of the input.
  • " " pushes an array containing 32 (for space).
  • * repeats that array as many times as the length of the input.
  • o displays the result as a list of characters.
\$\endgroup\$
0
\$\begingroup\$

Vyxal, 3 bytes

Lð*

Try it Online!

Command-line usage:

python3 Vyxal.py file_name "" input_string

Explained

L    # Push the length of the implicit input
  ð  # Push a space character
   * # Multiply that space by the length and output.
\$\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.