45
\$\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\$
    – manatwork
    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\$
    – Mayube
    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\$
    – manatwork
    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\$
    – Mayube
    Commented May 25, 2017 at 13:05
  • 1
    \$\begingroup\$ Can we assume the input will only have printable characters? \$\endgroup\$
    – Luis Mendo
    Commented May 25, 2017 at 13:30

138 Answers 138

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

Python 3, 23 bytes

print(' '*len(input()))

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

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

REXX 27 Bytes

say left("",length(arg(1)))
\$\endgroup\$
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
    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\$
    – Matt
    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!

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

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

dc, 18 bytes

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

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.