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
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"]
" "
" "
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.
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?
Bash + Coreutils, 11 Bytes
tr -c \ \
Powershell, 22 Bytes
' '*(Read-Host).length
-
1\$\begingroup\$ Not sure if
Read-Hostcounts for input but" "*"$args".lengthis shorter anyway. Also there is notlenproperty. That should be printing nulls. \$\endgroup\$Matt– Matt2017年05月30日 12:17:17 +00:00Commented May 30, 2017 at 12:17 -
\$\begingroup\$ Length property and Len are same \$\endgroup\$Sivaprasath Vadivel– Sivaprasath Vadivel2017年05月30日 13:32:32 +00:00Commented May 30, 2017 at 13:32
-
\$\begingroup\$
"hello".Lenis null on my system because that is a non-existent property where as"hello".Lengthreturns 5. If that works for you then you have an alias or extent that I, and most, do not have. \$\endgroup\$Matt– Matt2017年05月30日 14:14:55 +00:00Commented May 30, 2017 at 14:14 -
\$\begingroup\$ Ok...That explains it \$\endgroup\$Sivaprasath Vadivel– Sivaprasath Vadivel2017年05月30日 14:15:49 +00:00Commented May 30, 2017 at 14:15
-
\$\begingroup\$ 4 bytes
''"0outputs n newlines. \$\endgroup\$FrownyFrog– FrownyFrog2017年10月28日 01:07:05 +00:00Commented Oct 28, 2017 at 1:07 -
\$\begingroup\$ @FrownyFrog You should make your own answer, it's quite different from mine \$\endgroup\$Conor O'Brien– Conor O'Brien2017年10月28日 03:51:02 +00:00Commented Oct 28, 2017 at 3:51
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.
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!
-
1\$\begingroup\$ I do not know jq, but as
then""elseis syntactically valid, it seems like you could also omit the space in" " end. \$\endgroup\$Jonathan Frech– Jonathan Frech2017年09月19日 07:07:12 +00:00Commented Sep 19, 2017 at 7:07 -
1\$\begingroup\$ As variables probably cannot begin with a digit, the spaces in
<1 thenand<2 thenmay also be superfluous. \$\endgroup\$Jonathan Frech– Jonathan Frech2017年09月19日 07:40:23 +00:00Commented Sep 19, 2017 at 7:40
Vim, 4 bytes
VGr
(a trailing space)
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
C# .NET, 84 bytes
class P{static void Main(string[]a){System.Console.Write("".PadLeft(a[0].Length));}}
Kotlin, 20 bytes
{" ".repeat(length)}
{" ".repeat // repeat space
(length)} // length of input
Forth (gforth), 9 bytes
typewhite
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.
-
\$\begingroup\$ forth golfing language confirmed?! \$\endgroup\$Razetime– Razetime2020年11月17日 15:33:27 +00:00Commented Nov 17, 2020 at 15:33
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 containing32(for space).*repeats that array as many times as the length of the input.odisplays the result as a list of characters.
Vyxal, 3 bytes
Lð*
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.
Fig, \4ドル\log_{256}(96)\approx\$ 3.292 bytes
;M/
Note the space at the end.
;M/<space>
;M # Replace all in the input
/<space> # With a space
; # Print without newline
-
\$\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\$Dominic van Essen– Dominic van Essen2022年11月08日 13:34:04 +00:00Commented 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\$Seggan– Seggan2022年11月08日 13:36:08 +00:00Commented 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\$Dominic van Essen– Dominic van Essen2022年11月08日 13:37:24 +00:00Commented Nov 8, 2022 at 13:37
-
\$\begingroup\$ @DominicvanEssen all the other golflang answers seem to do that too... \$\endgroup\$Seggan– Seggan2022年11月08日 13:38:49 +00:00Commented Nov 8, 2022 at 13:38
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.
Thunno, \$ 6 \log_{256}(96) \approx \$ 4.94 bytes
L1-A_*
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
Thunno 2, 3 bytes
l−ṣ
Explanation
l−ṣ # Implicit input
l # Length
− # Decrement
ṣ # Spaces
# Implicit output with newline
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.
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\$