38
\$\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\$
  • 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 May 25 '17 at 12:56
  • 1
    \$\begingroup\$ @manatwork 0x0A and 0x20 are the hexadecimal values for the Newline and Space characters respectively \$\endgroup\$ – Skidsdev May 25 '17 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 May 25 '17 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\$ – Skidsdev May 25 '17 at 13:05
  • 1
    \$\begingroup\$ Got it. Thanks. \$\endgroup\$ – manatwork May 25 '17 at 13:06

102 Answers 102

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

Befunge-98 (PyFunge), 5 bytes

#@~a,

Try it online!

For every character in the input (#@~), this prints a new line (a,).

\$\endgroup\$
0
\$\begingroup\$

dc, 18 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

RProgN 2, 3 bytes

L•*

Uncreative solution is uncreative.

Just gets the length of the input, and multiplies , which is predefined to be a space, by it.

Try it online!

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

\$\endgroup\$
  • 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\$ – Jonathan Frech Sep 19 '17 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\$ – Jonathan Frech Sep 19 '17 at 7:40
0
\$\begingroup\$

Vim, 4 bytes

VGr 

(a trailing space)

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

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

C# .NET, 84 bytes

class P{static void Main(string[]a){System.Console.Write("".PadLeft(a[0].Length));}}

Try Online

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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