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
Cubix, 6 bytes
Wahoo a 6 byter!
wi?@oS
Cubified
w
i ? @ o
S
igets input?test top of stack- if negative (EOI) redirect onto
wlane change which umps to the@halt - if 0 (null) halt this shouldn't be hit
- if positive
Sowpush space to the stack, output and change lane ontoi
- if negative (EOI) redirect onto
-
1\$\begingroup\$ Sweet, it's not too often a Cubix program is this short :-) \$\endgroup\$ETHproductions– ETHproductions2017年05月27日 15:40:27 +00:00Commented May 27, 2017 at 15:40
C, 32 bytes
Try Online modifying characters into spaces
f(char*t){(*t=*t?32:0)&&f(t+1);}
C, 37 bytes
Try Online Left-padding the end-of-string with its length
f(char*t){printf("%*c",strlen(t),0);}
-
1
APL (Dyalog) 13.2, 1 byte
Prints only spaces.
∊ prototype (numbers become zeros, characters become spaces)
Pepe, (削除) 46 (削除ここまで) 37 bytes
REEeRREeeeREEEEErEeeEeeeeerEEeeeEreee
Explanation:
REEe # Input (string) in R
RREeee # Push reverse pointer position, or length of input - 1
# R flag: push in beginning
REEEEE # ...add 1
rEeeEeeeee # Push space in r
rEEeeeE # ...R times
reee # Output whole stack
Poetic, 81 bytes
the berenstein bears
i remember a series i spelled wrong
o m gee,i do remember it
The misspelling is intentional. (...or is it?)
Gema, 4 characters
?=\
(There is a space at the end of code.)
Sample run:
bash-4.4$ echo -n 'Hello, World!' | gema '?=\ '
bash-4.4$ echo -n 'Hello, World!' | gema '?=\ ' | wc
0 0 13
APL (Dyalog), 3 bytes
Prints only newlines.
0/⍪
⍪ table (makes string into column matrix)
0/ replicate each column zero times
Scala, 15 bytes
s=>" "*s.length
-
\$\begingroup\$ I created a Try it online for your program. \$\endgroup\$jrook– jrook2018年10月22日 05:40:55 +00:00Commented Oct 22, 2018 at 5:40
JavaScript (ES8), 22 bytes
s=>"".padEnd(s.length)
Java 8, 24 bytes
s->s.replaceAll("."," ")
Java 7, 49 bytes
String c(String s){return s.replaceAll("."," ");}
Chip, 2 bytes
*f
Chip reads in a byte, does whatever calculations are in the code, and writes a byte. So, for each byte of input, we ignore the input and write 0x20 instead. The empty Chip program would replace each byte of input with a null byte of output.
* Source element, activates any neighbor elements
f Output element for the bit 0x20, when active this bit is set in the output
Transposing the two characters would result in the same thing. I opted to use spaces, since 0x20 requires only one bit to be set. 0x0a requires setting two bits. Code for that could be:
b*d
Shell utils, (削除) 14 (削除ここまで) 12 bytes
tr ' -~' ' '
tr translates characters in the first parameter, into the corresponding one in the second parameter. (space)-~ is a range for space (32) to tilda (126), the first and last printable ASCII characters. They are mapped into a space; tr duplicates the last character in the output list if it is shorter than the input list.
-
3\$\begingroup\$ You could shave off a couple of bytes with different quoting styles (
\ -~instead of' -~') and you could even get away without quoting the first parameter at all if you use a control-character byte. \$\endgroup\$RJHunter– RJHunter2017年05月29日 03:53:39 +00:00Commented May 29, 2017 at 3:53 -
1\$\begingroup\$ That brings it down to 9 bytes:
tr ␁-~ \(␁=^A) \$\endgroup\$L3viathan– L3viathan2017年11月24日 08:11:42 +00:00Commented Nov 24, 2017 at 8:11
shortC, 16 bytes
f(C*a){W*a++)P'
Note the trailing space at end of code.
Conversions in this program:
C->charW->while(P->putchar(
The resulting program looks like this:
f(char *a){while(*a++)putchar(' ');}
How that works:
while(*a++)loops until it reaches the last index of the stringa.putchar(' ');prints a space for each index ofa.
dc, (削除) 25 (削除ここまで) 18 bytes
-1 byte thanks to brhfl
Z[1-d0<L32P]sLd0<L
Explanation:
Z[1-d0<L32P]sLd0<L
Implicit input
Z Get length
[ ]sL Create a funcion and saves in L
d0<L If length > 0, call L
1- Subtract 1 from the length
d0<L If length > 0, call L
32P Print space
-
\$\begingroup\$ Woah! It's worse than German. It would be neat if you explained how it works ;) \$\endgroup\$Maya– Maya2017年05月29日 17:01:47 +00:00Commented May 29, 2017 at 17:01
-
\$\begingroup\$ @NieDzejkob there :D \$\endgroup\$Felipe Nardi Batista– Felipe Nardi Batista2017年05月29日 17:57:48 +00:00Commented May 29, 2017 at 17:57
-
\$\begingroup\$ @Felipe You can shave off one byte by using the ASCII code point for a space instead of a string containing a space:
32Pinstead of[ ]P. I doubt it can be golfed down much further... \$\endgroup\$brhfl– brhfl2017年10月26日 17:14:47 +00:00Commented Oct 26, 2017 at 17:14 -
\$\begingroup\$ @brhfl didn't know i could do that, thanks \$\endgroup\$Felipe Nardi Batista– Felipe Nardi Batista2017年10月26日 18:43:11 +00:00Commented Oct 26, 2017 at 18:43
Ruby -p, 10 bytes
$_=$/*~/$/
Explanation:
$_= Output equals
$/ the output separator (defaults to newline)
* repeated a number of times equal to
~ the index in the input of the first match of
/$/ the regular expression for "end of line"
Powershell, (削除) 18 (削除ここまで) 12 bytes
-6 bytes thanks @Julian
$args|%{' '}
Testscript (use LF only mode in your editor):
$f = {
$args|% t*y|%{' '}
}
@(
,(13, "Hello, World!")
,(2, "Hi")
,(45, " Don't
Forget about
Existing
Whitespace! ")
,(0, "")
,(13, " ")
,(1,"
")
) | % {
$len,$source=$_
$r = &$f $source
$l=$r.length
"$($l-eq$len): $l"
}
Output:
True: 13
True: 2
True: 45
True: 0
True: 13
True: 1
Powershell + Regex, 20 bytes
$args-replace'.',' '
-
1\$\begingroup\$ You can go down 6 bytes using splatting ? Try it online! \$\endgroup\$Julian– Julian2021年04月14日 01:22:11 +00:00Commented Apr 14, 2021 at 1:22
-
1\$\begingroup\$ Thanks. Three years ago we didn't use splatting. Thanks again. \$\endgroup\$mazzy– mazzy2021年04月14日 04:34:18 +00:00Commented Apr 14, 2021 at 4:34
Trilangle, 11 bytes
i7#'@"o.:2L
Unfolds to this:
i
7 #
' @ "
o . : 2
L . . . .
This has to be the most creative way I've used @ yet: when hit in one direction, it ends the program; when hit in a different direction, it's divided by 2 to get a space.
Roughly equivalent to this C code:
#include <stdio.h>
int main() {
while (getchar() != EOF) {
printf(" ");
}
}
Bash, 16 bytes
printf %*s ${#1}
Uses parameter expansion count the length of the argument ${#1}, and then printf to output an empty string space-padded to that same length.
-
\$\begingroup\$
printf %${#1}sis 14 bytes;tr -c '' \(with input from the standard input stream) is 11 \$\endgroup\$набиячлэвэлиь– набиячлэвэлиь2022年12月20日 22:15:05 +00:00Commented Dec 20, 2022 at 22:15
-
\$\begingroup\$ .....I like it! \$\endgroup\$briantist– briantist2017年05月26日 04:08:44 +00:00Commented May 26, 2017 at 4:08
-
\$\begingroup\$ @briantist thanks for the TIO link, need to get into the habit of doing them myself. \$\endgroup\$colsw– colsw2017年05月26日 08:29:45 +00:00Commented May 26, 2017 at 8:29
-
\$\begingroup\$ can't go wrong, it creates the whole post for you. That's why I use it even when the code itself can't run in TIO 😂 \$\endgroup\$briantist– briantist2017年05月26日 11:56:29 +00:00Commented May 26, 2017 at 11:56
APL, (削除) 11 (削除ここまで) 6 bytes
5 bytes saved thanks to @Adám
' '⍴⍨≢
Uses the Dyalog Classical character set.
-
\$\begingroup\$ You can golf this significantly. First, swap the arguments of rho to remove the parentheses:
{' '⍴⍨⍴,⍵}, then use tally rather than rho to remove the comma:{' '⍴⍨≢⍵}, and finally, make it into a train to remove the braces and omega:' '⍴⍨≢. \$\endgroup\$Adám– Adám2017年05月25日 14:13:12 +00:00Commented May 25, 2017 at 14:13 -
\$\begingroup\$ No problem. I'll be happy to teach you more over in the APL chat room. \$\endgroup\$Adám– Adám2017年05月25日 14:33:40 +00:00Commented May 25, 2017 at 14:33
-
\$\begingroup\$ How is this 5 bytes? \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年05月26日 13:52:44 +00:00Commented May 26, 2017 at 13:52
-
\$\begingroup\$ @EriktheOutgolfer fixed, thanks. it counts 6 with the Dyalog char set \$\endgroup\$Uriel– Uriel2017年05月26日 14:11:51 +00:00Commented May 26, 2017 at 14:11
-
\$\begingroup\$ Save another byte by removing the space. \$\endgroup\$Adám– Adám2025年03月17日 09:53:39 +00:00Commented Mar 17 at 9:53
Perl 5, 6 + 1 = 7 bytes
Uses the -p flag.
y// /c
y/// is the transliteration operator: the first list is translated to the corresponding character in the second list. Without the c, this does nothing, but the c complements the first list, so all characters are transliterated to a space.
T-SQL, 32 bytes
SELECT SPACE(LEN(a+'x')-1)FROM t
Microsoft SQL's LEN function ignores trailing spaces, so this hacky workaround is required.
Input is stored in varchar column a in pre-existing table t, per our input rules.
q/kdb+, (削除) 14 (削除ここまで) 9 bytes
Solution:
{" "}each
Example:
q){" "}each"Hello, World"
" "
Explanation:
Returns " " for each character of the input.
Notes:
I've made a shorter version (7 bytes) that does something similar:
{y}'" "
... but you have to prepend the input rather than append:
q)"hello world"{y}'" "
" "
-
\$\begingroup\$ Using one of the k's, 2 bytes is possible as
"", e.g. this ngn/k version. \$\endgroup\$coltim– coltim2020年11月17日 17:16:33 +00:00Commented Nov 17, 2020 at 17:16
TI-Basic, 13 bytes
For(I,2,length(Ans
Disp "
End
Loop starts at 2 because an additional newline is printed before Done at the end of the program.
MATL, (削除) 3 (削除ここまで) 4 bytes
nqZ"
Today I learnt that MATLAB has a function for creating a string of spaces!
n - count the number of bytes in input string
q - decrement by 1, because the implicit disp at the end adds a newline (so the number of spaces required is string length - 1)
Z" - blanks command: create a string with the specified number of spaces in it
(Implicit output at end, with trailing newline.)
-
\$\begingroup\$ apart from using
gsub,strrepexists as well, sostrrep(" ",nchar(scan,"")))would work (even though it's longer than @ngm 's suggestion) \$\endgroup\$Giuseppe– Giuseppe2018年07月10日 20:27:17 +00:00Commented Jul 10, 2018 at 20:27 -
\$\begingroup\$ @Giuseppe Is this a recent function? I can't believe I'm hearing about it for the first time. \$\endgroup\$JayCe– JayCe2018年07月10日 20:37:07 +00:00Commented Jul 10, 2018 at 20:37
-
\$\begingroup\$ It appears to have been added in
3.3.0. I haven't really found a great use for it; usually the first thing I do when I see a string is toutf8ToIntit and then I can just userepfor mostly the same purpose, andrephas finer control beyond justtimes, witheach,length.out\$\endgroup\$Giuseppe– Giuseppe2018年07月10日 21:01:08 +00:00Commented Jul 10, 2018 at 21:01
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\$