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
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"]
" "
" "
answered May 27, 2017 at 1:29
\$\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.

answered May 29, 2017 at 5:47
\$\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?

answered May 30, 2017 at 6:08
\$\endgroup\$
0
\$\begingroup\$

Bash + Coreutils, 11 Bytes

tr -c \ \

answered May 30, 2017 at 18:41
\$\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\$ Commented May 30, 2017 at 12:17
  • \$\begingroup\$ Length property and Len are same \$\endgroup\$ Commented May 30, 2017 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\$ Commented May 30, 2017 at 14:14
  • \$\begingroup\$ Ok...That explains it \$\endgroup\$ Commented May 30, 2017 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!

answered May 30, 2017 at 8:05
\$\endgroup\$
0
0
\$\begingroup\$

J, 6 bytes

' '#~#

Try it online!

Explanation

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

Common Lisp, 32 bytes

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

Try it online!

answered Jul 20, 2017 at 15:29
\$\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.

answered Jul 20, 2017 at 15:30
\$\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)
answered Sep 19, 2017 at 2:12
\$\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!

answered Sep 19, 2017 at 6:57
\$\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\$ Commented Sep 19, 2017 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\$ Commented Sep 19, 2017 at 7:40
0
\$\begingroup\$

Vim, 4 bytes

VGr 

(a trailing space)

answered Dec 12, 2017 at 12:40
\$\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!

answered Jul 10, 2018 at 1:26
\$\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!

answered Mar 28, 2020 at 9:39
\$\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.

answered Nov 2, 2020 at 2:20
\$\endgroup\$
1
  • \$\begingroup\$ forth golfing language confirmed?! \$\endgroup\$ Commented Nov 17, 2020 at 15:33
0
\$\begingroup\$

Add++, 21 bytes

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

Try it online!

answered Nov 17, 2020 at 12:44
\$\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.
The Fifth Marshal
6,2631 gold badge27 silver badges46 bronze badges
answered May 30, 2017 at 16:59
\$\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.
answered Sep 29, 2020 at 2:02
\$\endgroup\$
0
\$\begingroup\$
answered Apr 15, 2021 at 18:49
\$\endgroup\$
0
\$\begingroup\$

Factor, 20 bytes

[ [ drop bl ] each ]

Try it online!

For each character in the input string, drop it from the data stack and output a space.

answered Oct 15, 2022 at 5:38
\$\endgroup\$
0
\$\begingroup\$

Fig, \4ドル\log_{256}(96)\approx\$ 3.292 bytes

;M/ 

Try it online!

Note the space at the end.

;M/<space>
;M # Replace all in the input
 /<space> # With a space
; # Print without newline
answered Nov 3, 2022 at 16:55
\$\endgroup\$
4
  • \$\begingroup\$ Are you sure this doesn't output a trailing newline? This is what it looks like when I run it... (I only checked because the same annoying thing was happening for my own attempt in Nibbles...) \$\endgroup\$ Commented Nov 8, 2022 at 13:34
  • \$\begingroup\$ @DominicvanEssen ah yes, it does output with a newline. I missed that part of the spec. will update \$\endgroup\$ Commented Nov 8, 2022 at 13:36
  • \$\begingroup\$ If you can use the same code in the context of a function, maybe the returned string would be Ok... \$\endgroup\$ Commented Nov 8, 2022 at 13:37
  • \$\begingroup\$ @DominicvanEssen all the other golflang answers seem to do that too... \$\endgroup\$ Commented Nov 8, 2022 at 13:38
0
\$\begingroup\$

Pascal, 82 B

This program merely requires a processor complying to ISO standard 7185 "Pascal". Since it is not guaranteed that writeLn produces exactly one character (on some platforms the line separator is CR+LF), we’ll have to use write(' '):

program p(input,output);begin while not EOF do begin write(' ');get(input)end end.

If input was simply a string value, when an ISO standard 10206 "Extended Pascal" processor is used the following is the shortest solution (≥ 24 B):

writeStr(outputStringVariable, '':length(inputMyString))

The :someNonNegativeIntegerValue after the empty string '' will (left) pad the given argument to the specified width using space characters (' '). A complete demonstration:

program paddingDemo(output); begin write('':length('Hello, World!')) end.
answered Nov 8, 2022 at 14:40
\$\endgroup\$
0
\$\begingroup\$

Thunno, \$ 6 \log_{256}(96) \approx \$ 4.94 bytes

L1-A_*

Attempt This Online!

Note: A\ will also work instead of A_ if you want newlines rather than spaces.

Explanation

L1-A_* # Implicit input
L # Length
 1- # Minus one
 * # Multiplied by
 A_ # A space
 # Implicit print
 # with a newline
answered Mar 15, 2023 at 12:55
\$\endgroup\$
0
\$\begingroup\$

Thunno 2, 3 bytes

l−ṣ

Attempt This Online!

Explanation

l−ṣ # Implicit input
l # Length
 − # Decrement
 ṣ # Spaces
 # Implicit output with newline
answered Jun 3, 2023 at 18:03
\$\endgroup\$
0
\$\begingroup\$

Swift 6, 36 bytes

{{String.init}()(" ",(0ドル+"").count)}

To use this, assign it to something and then call that thing:

let whitespace = {{String.init}()(" ",(0ドル+"").count)}
print(whitespace("Hello, world!"), terminator: "")
// Prints " "

The {String.init}() trickery simply gives us a reference to String.init(repeating:count:), but without the costly argument labels. Appending an empty string to 0ドル (the implicit closure parameter) informs the type checker that 0ドル is a String.

answered Mar 1 at 22:03
\$\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.