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!

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

Z80Golf, 9 bytes

00000000: d5cd 0380 3001 763e 20 ....0.v> 

Try it online!

Disassembly:

 push de ; push $0000 call $8003 ; getchar(A) jr nc, k ; jump if not EOF halt k:ld a, ' ' ; replace A with space ; memory from $000a through $7fff is $00=NOP... ; PC reaches $8000=putchar(A) and RETs to pushed address. 

(If you remove the last two bytes, this is a cat program.)

\$\endgroup\$
1
\$\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\$
1
\$\begingroup\$

dc, 12 bytes

Z256r^25.5/P 

Try it online!

Takes input from the stack, outputs newlines to stdin.

I know there's two dc answers here already, but this one uses a different approach, with math! Plus it's 6 bytes shorter, so I guess that's all right.

Explanation

One of dc's three explicit printing commands is P, which takes a number and outputs it as a base 256 (technically base UCHAR_MAX+1, works on my machine) byte stream. So I need to feed it the number (where n is the length of the given string, and 10 is the codepoint of the linefeed character):

 10*256^(n-1) + 10*256^(n-2) + ... + 10 = 10 * (256^(n-1) + 256^(n-2) + ... + 1) (factoring out 10) = 10 * (256^n - 1) / (256 - 1) (geometric series formula) = (256^n - 1) / 25.5 (combining constants) ~= (256^n) / 25.5 (because dc's default precision is 0) 

The code is a straightforward calculation of this number, followed by P.

\$\endgroup\$
2
  • \$\begingroup\$ Argh, doesn't handle the empty string (P outputs a NUL when it pops a 0). I guess I could say it's outputting a null-terminated string...seems like a stretch though. \$\endgroup\$ Commented Jul 10, 2018 at 19:43
  • \$\begingroup\$ Also, it's only on TIO that you can tell. On my machine a NUL gets eaten silently so it works. I still feel badly about it. \$\endgroup\$ Commented Jul 10, 2018 at 19:57
1
\$\begingroup\$

Octave / Matlab, 25 23 bytes

@(x)repmat(' ',size(x)) 

Saved 2 bytes thanks to Giuseppe

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ I think the f= is unnecessary; anonymous functions / function handles are perfectly acceptable on PPCG. \$\endgroup\$ Commented Jul 10, 2018 at 22:27
  • \$\begingroup\$ You can also include a link to Try it online! for Octave so others can test your answer. \$\endgroup\$ Commented Jul 10, 2018 at 22:28
1
\$\begingroup\$

Java (JDK), 84 bytes

static String m(String n){int i=0;String d="";while(i++<n.length())d+=" ";return d;} 

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Updated Sir @JoKing. \$\endgroup\$ Commented Oct 22, 2018 at 12:08
  • \$\begingroup\$ This does not address my other concerns, i.e. initialising d and reusing the function. Also, wouldn't it be shorter to use an anonymous lambda? \$\endgroup\$ Commented Oct 22, 2018 at 12:14
  • \$\begingroup\$ Your suggestions are so useful, Thanks @JoKing \$\endgroup\$ Commented Oct 23, 2018 at 5:07
1
\$\begingroup\$

Wren, 22 bytes

Multiply the space by the length of the string.

Fn.new{|a|" "*a.count} 

Try it online!

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

Keg,-lp -ir, 3 bytes

( , 

Try it online!

This takes input as characters and prints a space for each character The -lp flag makes the length() function take input if the stack is empty and the -ir flag ensures that the implicit input is as characters.

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

x86-16 ASM, IBM PC DOS, 11 bytes

Binary:

00000000: 8a0e 8000 49b8 200a cd10 c3 ....I. .... 

Unassembled:

D1 EE SHR SI, 1 ; SI to 80H (SI intialized at 100H) AC LODSB ; load string length into AL 91 XCHG AX, CX ; put input string length into CX 49 DEC CX ; remove leading whitespace from length AC LODSB ; load whitespace delimiter into AL B4 0A MOV AH, 0AH ; BIOS "write character CX number of times" function CD 10 INT 10H ; call BIOS, display to console C3 RET ; return to DOS 

Explanation:

Input is via command line, though all that's important is the length. Command line input length is always stored at memory address DS:0080H in DOS, so put that into CX. DOS includes the space between the executable name and the command line args string in this number.

For example: in FOO.COM Hello, length is 6 and command line string is " Hello", or calling as FOO.COM/Hello, command line string is "/Hello" (Note: those are the the only valid characters for the character immediately after the executable name). This first character (will be a space when called normally) is what is displayed as the "invisible text" for output. This builds in a handy little "debug mode" where you can use a slash instead of a space to actually be able to test your output is the right length.

Then, use the IBM PC BIOS's INT 10H "Write character only at cursor position" (0AH) function that writes the same character CX number of times.

Example Output:

Admittedly, displaying 13 chars of whitespace is not very interesting in a screenshot. However, by using a slash instead of a space ("debug mode") you can actually see that you are displaying the right number of chars.

enter image description here

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

naz, 40 bytes

2a2x1v1x1f1r3x1v2e0m4a8m1o1f0x1x2f0a0x1f 

Works for any input string terminated with the control character STX (U+0002).

Explanation (with 0x commands removed)

2a2x1v # Set variable 1 equal to 2 1x1f1r3x1v2e # Function 1 # Read a byte of input # Jump to function 2 if it equals variable 1 0m4a8m1o1f # Otherwise, output a space and jump back to the start of function 1 1x2f0a # Function 2 # Add 0 to the register 1f # Call function 1 
\$\endgroup\$
1
\$\begingroup\$

GolfScript, 5 bytes

Port of CJam answer.

," "* 

Try it online!

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

Ahead, 12 bytes

S0d3-' \k:W@ 

Try it online!

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

Java, 24 bytes

s->s.replaceAll("."," ") 

TIO

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

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\$ Commented Nov 17, 2020 at 18:06
1
\$\begingroup\$

Vyxal, 2 bytes

LI 

Try it Online!

Vyxal S, 2 bytes

Try it Online!

If we want to port the Jelly answer, then:

Vyxal rs, 3 bytes

fð• 

Try it Online!

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

C (gcc), 50 36 bytes

Totally pays off that printf doesn't automatically print a newline.

36 bytes by ceilingcat

f(char*n){for(;printf(" "+!*n++););} 
\$\endgroup\$
0
1
\$\begingroup\$

Nibbles, 2.5 bytes (5 nibbles)

.$' ' 

A function that returns a string of spaces the same length as its argument.
(This won't work as a full program, since Nibbles will write a newline at the end of its output).

. # map over $ # characters of the argument ' ' # returning a space for each 

Obviously the output is invisible, but we can call this function from within a program that wraps the output into quotes to see the output string:

;~ # save this function: .$' ' # <convert to invisible text> :'"'$ # prepend saved function results with a quote : '"' # and append that with another quote 

enter image description here


Nibbles, 3.5 bytes (7 nibbles)

>>.$' ' 

Full program. We can't stop Nibbles from writing the trailing newline at the end of its output, so we shorten the invisible string by one character.

 . # map over $ # characters of the argument ' ' # returning a space for each >> # and then remove the first character 
\$\endgroup\$
1
\$\begingroup\$

Python 3, 36 bytes

lambda x:re.sub('.',' ',x) import re 

Try it online!

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

Go, 66 bytes

import."strings" func f(s string)string{return Repeat(" ",len(s))} 

Attempt This Online!

As far as I'm aware, you can't multiply strings and numbers to repeat a string in Go - you have to use strings.Repeat.

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

(,) 40 34 Chars or \$34\log_{256}(3)\approx6.74\$ Bytes

(,,,()()()()()()()()()(),((()()))) 

Or, printing spaces (54 Chars or 10.7 Bytes):

((),()()()()()()()())(,,,(())(())(())(()),((()())),()) 
\$\endgroup\$
1
\$\begingroup\$

TI-BASIC, 27 bytes

Input Str1 For(X,1,length(Str1 Disp " End 

I love TI-84 because it completes my parentheses and quotes for me.
At the same time, it is quite annoying indeed how there is no string repeat (as far as I know). Even more annoying is that (as far as I know) all input is int not str and any text passed will result in a single newline. I hope this is still valid despite that. That was my bad. I was using an int variable.

Bytecount on TI-BASIC is kind of complex, the best way that I know of is going to TI Connect [CE] and looking for your program there.

Explained:

Input Str1 Ask for input and store in string variable length(Str1 Length of Str1 For(X,1,... Loop through following code that many times Disp " Display a blank string (with newline) End End loop, go back to the For. 

Test cases:

Typing on a TI-84 Plus CE is hard.
In these examples "-" is displayed instead of newline so you don't have to count. That took a while to type.enter image description here

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

Zsh, 11 bytes

tr -c \ \ 

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ probably ought to use printf because of the trailing newline \$\endgroup\$ Commented Feb 17 at 5:32
1
\$\begingroup\$

AWK, 12 10 bytes

NR>1,$0=FS 

Attempt This Online!

gsub(/./,FS) 

Attempt This Online!

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

Plain TeX, 87 bytes

\def\p#1{\phantom{#1}\egroup}{\obeylines\gdef~{\bgroup\obeylines\obeyspaces\def { }\p}} 

(including the carriage return).

Explanations

The typesetting program TeX has a command \phantom to generate invisible contents. We can then simply define a command with

\def~#1{\phantom{#1}} 

which corresponds to 21 bytes.

The problem is that TeX aggregate successive spaces (SPC) and ignore carriage return (CR) in command arguments. To change that, we can add

\obeylines \obeyspaces \def^^M{ } 

where ^^M gives the ASCII code of the carriage return (CR).

This works, but alters the meaning of SPC and CR in all the rest of the document. Avoiding that is a bit more tricky and needs and auxiliary macro

{% open a group \obeylines% make carriage return char an 'active' character \gdef~{% define ~ globally \bgroup% open a group \obeylines% in this group, make SPC \obeyspaces% and CR be active and act normally \def { }% define the CR to be a space (no % at end of the previous line) \p% call \p }% end of ~ definition }% close the current group: SPC and CR retrieve their standard definitions \def\p#1{% \phantom{#1}% generate invisible content \egroup% close the current group } 

Test code

\def\p#1{\phantom{#1}\egroup}{\obeylines\gdef~{\bgroup\obeylines\obeyspaces\def { }\p}} \tt \frenchspacing >1234567890123< 13 spaces\par >~{Hello, World!}< \vskip 1em >12< 2 spaces\par >~{Hi}< \vskip 1em >123456789012345678901234567890123456789012345< 45 spaces\par >~{ Don't Forget about Existing Whitespace! }< \vskip 1em >< 0 space\par >~{}< \vskip 1em >1234567890123< 13 spaces\par >~{ }< \vskip 1em >1< 1 space\par >~{ }< \bye 

Example

Note we need \tt to get a monospaced font (typewriter for TeX) and \frenchspacing to get spaces after punctuation marks (such as the !) have the same width (TeX is designed to typeset documents, not for code golfing).

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

BrainChild, 48 37 bytes

include*;gets("")(int c=>putchar(32)) 

Try It Online!

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

Janet, 24 bytes

|(peg/replace-all 1" "$) 
\$\endgroup\$
1
\$\begingroup\$

SAKO, 103 bytes

CALKOWITE:*W,I BLOK(9):W CZYTAJWIERSZ:W *)GDYW(I)=58:1,INACZEJ2 2)POWTORZ:I=0(1)9 1)LINIII STOP1 KONIEC 

Doesn't really work on strings longer than 9 characters, for that it would need more bytes.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Is nine the maximum integer value in this language? If not, I think you would have to allow strings longer than 9 chars. \$\endgroup\$ Commented Mar 19 at 11:26
  • \$\begingroup\$ @noodleperson No, this isn't the maximum in this language. It would depend on the architecture. For example ZAM-2 had 18 bit integers while ZAM-41 had 24 bit ones. The limit is 9 because I defined an array for containing the string as 10 integers long. (Last one is for ending the string). I could increase it indefinitely depending on amount of memory that the machine possesses, so I didn't see any value in making it larger at the same time increasing my byte count. \$\endgroup\$ Commented Mar 19 at 11:36
0
\$\begingroup\$

REXX 27 Bytes

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.