33
\$\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
  • 2
    \$\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

93 Answers 93

4
\$\begingroup\$

Pyth, 3 bytes

*dl

Try it!

Python equivalent: len(input())*" "

jmk

Try this!

Python equivalent: "\n".join(map("", input()))

smb

Try that!

Python equivalent: "".join(map("\n",input())

VQk

Try!

Python equivalent: For N in input():print("")

\$\endgroup\$
  • \$\begingroup\$ @StanStrum: It outputs newlines. Newlines are also allowed per the challenge description above. You can see this better from this example input \$\endgroup\$ – KarlKastor Sep 18 '17 at 19:41
3
\$\begingroup\$

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

Jelly, 2 bytes

⁶ṁ

Try it online!

\$\endgroup\$
3
\$\begingroup\$

APL (Dyalog), 3 bytes

Prints only newlines.

0/⍪

Try it online!

 table (makes string into column matrix)

0/ replicate each column zero times

\$\endgroup\$
3
\$\begingroup\$

Scala, 15 bytes

s=>" "*s.length
\$\endgroup\$
  • \$\begingroup\$ I created a Try it online for your program. \$\endgroup\$ – jrook Oct 22 '18 at 5:40
3
\$\begingroup\$

C (tcc), 31 bytes

I opted to output newlines since it's shorter...

f(char*s){for(;*s++;puts(""));}

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Java 8, 24 bytes

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

Try it here.

Java 7, 49 bytes

String c(String s){return s.replaceAll("."," ");}

Try it here.

\$\endgroup\$
3
\$\begingroup\$

Chip, 2 bytes

*f

Try it online!

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

shortC, 16 bytes

f(C*a){W*a++)P' 

Note the trailing space at end of code.

Conversions in this program:

  • C -> char
  • W -> 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 string a.
  • putchar(' '); prints a space for each index of a.

Try it online!

\$\endgroup\$
3
\$\begingroup\$

dc, 25 18 bytes

-1 byte thanks to brhfl

Z[1-d0<L32P]sLd0<L

Try it online!

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
\$\endgroup\$
  • \$\begingroup\$ Woah! It's worse than German. It would be neat if you explained how it works ;) \$\endgroup\$ – NieDzejkob May 29 '17 at 17:01
  • \$\begingroup\$ @NieDzejkob there :D \$\endgroup\$ – Felipe Nardi Batista May 29 '17 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: 32P instead of [ ]P. I doubt it can be golfed down much further... \$\endgroup\$ – brhfl Oct 26 '17 at 17:14
  • \$\begingroup\$ @brhfl didn't know i could do that, thanks \$\endgroup\$ – Felipe Nardi Batista Oct 26 '17 at 18:43
3
\$\begingroup\$

Pepe, 46 37 bytes

REEeRREeeeREEEEErEeeEeeeeerEEeeeEreee

Try it online!

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

Perl 6, 10 bytes

{S:g/./ /}

Basic string substitution.

\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES8), 22 bytes

s=>"".padEnd(s.length)
\$\endgroup\$
2
\$\begingroup\$

Convex, 2 bytes

,*

Try it online!

Simply takes the length of the input and multiplies by newlines (which are at the bottom of the stack)

\$\endgroup\$
2
\$\begingroup\$

Bash, 16 bytes

printf %*s ${#1}

Try it online!

Uses parameter expansion count the length of the argument ${#1}, and then printf to output an empty string space-padded to that same length.

\$\endgroup\$
2
\$\begingroup\$

PowerShell, 21 bytes

[char[]]"$args"|%{""}

Try it online!

prints newlines.

\$\endgroup\$
2
\$\begingroup\$

APL, 11 6 bytes

5 bytes saved thanks to @Adám

' '⍴⍨≢

Uses the Dyalog Classical character set.

\$\endgroup\$
  • \$\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 May 25 '17 at 14:13
  • \$\begingroup\$ @Adám thanks! never seen the tally before \$\endgroup\$ – Uriel May 25 '17 at 14:32
  • \$\begingroup\$ No problem. I'll be happy to teach you more over in the APL chat room. \$\endgroup\$ – Adám May 25 '17 at 14:33
  • \$\begingroup\$ How is this 5 bytes? \$\endgroup\$ – Erik the Outgolfer May 26 '17 at 13:52
  • \$\begingroup\$ @EriktheOutgolfer fixed, thanks. it counts 6 with the Dyalog char set \$\endgroup\$ – Uriel May 26 '17 at 14:11
2
\$\begingroup\$

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.

\$\endgroup\$
  • 2
    \$\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 May 29 '17 at 3:53
  • \$\begingroup\$ That brings it down to 9 bytes: tr ␁-~ \ (␁=^A) \$\endgroup\$ – L3viathan Nov 24 '17 at 8:11
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

Aceto, 5 bytes

Trivial in Aceto:

p
,'O

Reads a character, pushes a space, prints it, and goes back to the start.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

MATL, 3 4 bytes

nqZ"

Try it online!

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.)

\$\endgroup\$
2
\$\begingroup\$

R, 37 23 bytes

gsub("."," ",scan(,""))

Try it online!

14 bytes saved by ngm.

\$\endgroup\$
  • \$\begingroup\$ apart from using gsub, strrep exists as well, so strrep(" ",nchar(scan,""))) would work (even though it's longer than @ngm 's suggestion) \$\endgroup\$ – Giuseppe Jul 10 '18 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 Jul 10 '18 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 to utf8ToInt it and then I can just use rep for mostly the same purpose, and rep has finer control beyond just times, with each, length.out \$\endgroup\$ – Giuseppe Jul 10 '18 at 21:01
1
\$\begingroup\$

SmileBASIC 3, 22 bytes

Asks for a string from the console as input, then prints length spaces. PRINT (or ? here) adds a trailing newline by default, so we use the ; to disable it.

LINPUT A$?" "*LEN(A$);
\$\endgroup\$
  • \$\begingroup\$ This technically isn't valid since LINPUT doesn't accept newlines, so you'll have to define a function (also ; isn't required because PRINT doesn't really output newlines) \$\endgroup\$ – 12Me21 Mar 3 '18 at 16:18
1
\$\begingroup\$

Mathematica, 30 bytes

Row@Table[" ",StringLength@#]&
\$\endgroup\$
1
\$\begingroup\$

Lua, 25 bytes

for i=1,#...do
print()end

Same length as:

io.write((' '):rep(#...))

Try it online!

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

tcl, 19

regsub -all . $s \ 

demo

To test it, click "Run it" button and then select the white space on the white bottom area. A better test is to add a space and a letter before the ] as I describe:

puts [regsub -all . $s \  x]    
                        ^^ Two spaces here

and it will output the count of characters of each string exactly equal to the ones on the question.

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

Rust, 23 bytes

|s|" ".repeat(s.len());

First time using Rust so not 100% sure I've got everything correct, let me know if I need to change anything. I couldn't work out how to test this, as I'm still new to it, but judging from the documentation it should work. Also any improvements are more than welcome!

\$\endgroup\$
  • 1
    \$\begingroup\$ Hi fellow newbie! I tried to rig up enough surrounding code to make a Try It Online link for this answer (and any similar Rust entries). I did get somewhere but I needed to add a type declaration at the cost of an extra 5 bytes (for a total of 28 bytes) to get it to compile. \$\endgroup\$ – RJHunter May 26 '17 at 3:48
1
\$\begingroup\$

Brain-Flak, 28 bytes

{{}<>((()()()()()){})<>}<>{}

Try it online!

{{}                    }     # For every input character...
     ((()()()()()){})        #    Push 10...
   <>                <>      #    on the other stack
                        <>   # Switch to the stack with all of the newlines
                          {} # Pop a newline because the interpreter prints a newline :(
\$\endgroup\$
1
\$\begingroup\$

pb - 17 bytes

^w[B!0]{>}<vb[32]

Goes to the last character of the input and puts a space on the canvas cell representing it. Because output in pb is 2D, the empty cells before it are automatically filled in with spaces when it's outputted.

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