27
\$\begingroup\$

I've already made this in Python, but it seems that it could be shortened a lot:

txt = input("Type something.. ")
c = "#"
b = " "
print(c * (len(txt) + 4))
print(c, b * len(txt), c)
print(c, txt, c)
print(c, b * len(txt), c)
print(c * (len(txt) + 4))

So if the user types:

Hello World

The program prints:

###############
#             #
# Hello World #
#             #
###############


Fewest bytes wins—and of course, the answer can be written in any language.

\$\endgroup\$
10
  • \$\begingroup\$ The same challenge as editor golf. \$\endgroup\$ Commented Sep 13, 2015 at 12:44
  • 1
    \$\begingroup\$ Not so different: codegolf.stackexchange.com/questions/57442/show-tree-rings-age/… \$\endgroup\$
    – edc65
    Commented Sep 13, 2015 at 13:40
  • 1
    \$\begingroup\$ The input string won't contain linebreaks, right? \$\endgroup\$
    – flodel
    Commented Sep 13, 2015 at 13:51
  • 2
    \$\begingroup\$ @edc65 I disagree, this challenge is hugely different \$\endgroup\$
    – Beta Decay
    Commented Sep 13, 2015 at 13:55
  • 14
    \$\begingroup\$ I would recommend to wait at least a week before accepting an answer. While it doesn't really matter if you are planning to update the accepted answer if a shorter submission comes in, there will be people complaining about an early accepted answer, or even downvote it. There will also be some people who won't be interested in posting an answer if there is already an accepted one. \$\endgroup\$ Commented Sep 13, 2015 at 17:36

46 Answers 46

1
2
1
\$\begingroup\$

APL (Dyalog Unicode), 19 bytes

(⍉⍪⍪⊣)/'##  ',⊂∘⍉∘⍪

Try it online!

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

Stax, 14 bytes

≡┼ö▓ú¡êqct╜R"Γ

Run and debug it

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

Thunno 2 N, 16 bytes

ðṛ[l'#×;lṣ;]'#ṛⱮ

Try it online!

Explanation

ðṛ[l'#×;lṣ;]'#ṛⱮ  # Implicit input
ðṛ                # Surround by spaces
  [    ;  ;]      # Apply parallelly, and wrap into a list:
   l'#×          '#   [0]  length * "#"
        lṣ        #   [1]  length * " "
                  #   [2]  leave TOS
            '#ṛ  '# Surround each by "#"s
               Ɱ  # Palindromise the list
\$\endgroup\$
1
\$\begingroup\$

Ruby -nl, 53 bytes

s="# %s #"
puts h=?#*(4+l= ~/$/),s%b=' '*l,s%$_,s%b,h

Attempt This Online!

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

PowerShell, 84 82 bytes

$l=$input.length+4;$p='#'*$l;$s=' '*$l;$p,$s,"  $input  ",$s,$p-replace'^ | $','#'
\$\endgroup\$
0
\$\begingroup\$

Lua, 90 bytes

a=arg[1]h="#"s=" "t="\n"..h c=h:rep(#a+4)..t..s:rep(#a+2)..h..t..s print(c..a..c:reverse())
\$\endgroup\$
0
\$\begingroup\$

Racket 172 bytes

(λ(s)(let((n(string-length s))(p #\space)(g(λ(l c)(make-string l c))))(display(string-append
(g(+ 4 n)#\#)"\n#"(g(+ 2 n)p)"#\n# "s" #\n#"(g(+ 2 n)p)"#\n"(g(+ 4 n)#\#)))))

Ungolfed:

(define (f s)
  (let ((n (string-length s))
        (p #\space)
        (g (λ (l c) (make-string l c))))
    (display (string-append (g (+ 4 n) #\#)
                            "\n#"
                            (g (+ 2 n) p)
                            "#\n# "
                            s
                            " #\n#"
                            (g (+ 2 n) p)
                            "#\n"
                            (g (+ 4 n) #\#)
                            ))))

Testing:

(f "This is a test" )

Output:

##################
#                #
# This is a test #
#                #
##################
\$\endgroup\$
0
\$\begingroup\$

C#, 116 110 bytes

s=>{string r="\n".PadLeft(s.Length+5,'#'),p="#".PadRight(s.Length+3,' ')+"#\n";return r+p+"# "+s+" #\n"+p+r;};

Ungolfed:

s=>
{
    string r = "\n".PadLeft(s.Length + 5, '#'),         //the first line, made of '#'s
        p = "#".PadRight(s.Length + 3, ' ') + "#\n";    //the second line
    return r + p + "# " + s + " #\n" + p + r;           //output is simmetrical
};

Initial version:

s=>{int l=s.Length;string r=new string('#',l+4)+"\n",p="#"+new string(' ',l+2)+"#\n";return r+p+"# "+s+" #\n"+p+r;};

Full program with test cases:

using System;

namespace SurroundStringWithHashes
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<string,string>f= s=>{int l=s.Length;string r=new string('#',l+4)+"\n",p="#"+new string(' ',l+2)+"#\n";return r+p+"# "+s+" #\n"+p+r;};

            Console.WriteLine(f("Hello World"));
            Console.WriteLine(f("Programming Puzzles & Code Golf"));
        }
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ Use var instead of string. \$\endgroup\$
    – Yytsi
    Commented Oct 8, 2016 at 22:58
  • \$\begingroup\$ Won't help in this case, since I have 2 strings and each var keyword allows only one declaration. \$\endgroup\$
    – adrianmp
    Commented Oct 8, 2016 at 23:02
  • \$\begingroup\$ Oops, didn't see the second one :D \$\endgroup\$
    – Yytsi
    Commented Oct 9, 2016 at 8:07
0
\$\begingroup\$

C (gcc) 165 bytes

f(*s){i,j;c='#';for(j=0;j<5;j++){if(j==0||j==4){for(i=0;i<strlen(s)+2;i++)printf("#");}else if(j==2) printf("#%s#",s);else printf("#%*c",(strlen(s)+1),c);puts("");

Ungolfed version

void  f(char *s)
{
    int i,j;
    char c='#';

    for(j=0;j<5;j++)
    { 
       if(j==0||j==4)
       { 
         for(i=0;i<strlen(s)+2;i++)
           printf("#");
       }
       else
        if(j==2)
         printf("#%s#",s);
      else
        printf("#%*c",(int)(strlen(s)+1),c);

   puts("");
}
\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 73 bytes

LINPUT S$L=LEN(S$)+2O$="#"+" "*L+"#
T$="#"*(L+2)?T$?O$?"# ";S$;" #
?O$?T$
\$\endgroup\$
0
\$\begingroup\$

Python 3, 66 bytes

lambda x:("###### %s ######\n\n\n\n"%' ## '.join(f" {x} ")*5)[::5]

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ I would recommend splitting this into two separate answers, as the languages are very different. Also, non-competitive answers are no longer a thing, so don't worry about the new features \$\endgroup\$ Commented Aug 7, 2020 at 16:43
  • \$\begingroup\$ Thanks for the advice. Didn't know that non-competitive wasn't a thing anymore, just saw that one of the other answers mentioned it. I'll move the Haskell one to a different answer. \$\endgroup\$
    – Kyuuhachi
    Commented Aug 7, 2020 at 18:43
  • \$\begingroup\$ Here is a version that takes input and prints. It costs 4 bytes. print(("###### %s ######\n\n\n\n"%' ## '.join(f" {input()} ")*5)[::5]) \$\endgroup\$
    – Hunaphu
    Commented Jun 28, 2021 at 10:49
0
\$\begingroup\$

Regenerate, 38 bytes

(\#{#~1+4})(\n# {#~1+2}#\n)# $~1 #$2$1

Takes the string as a command-line argument. Try it here!

Explanation

Spaces are replaced by underscores so they're easier to see.

(         )                             Group 1: top row
 \#                                      A literal hash character
   {     }                               repeated
    #~1                                  len(input)
       +4                                + 4 times
           (              )             Group 2: second row (plus newlines)
            \n#                          Newline followed by hash
               _{     }                  followed by space repeated
                 #~1+2                   len(input) + 2 times
                       #\n               followed by hash and another newline
                                        Middle row:
                           #_            Hash followed by space
                             $~1         followed by input
                                _#       followed by space and another hash
                                        Final two rows:
                                  $2     Group 2 again
                                    $1   Group 1 again
\$\endgroup\$
0
\$\begingroup\$

Japt -h, 17 bytes

8Æ=z ®i" #"gXz4}R

Try it

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

Python 3, 68 bytes

s=input()
n=len(s)+2
v='#'*n+'##\n#'+' '*n+'#\n#'
print(v,s,v[::-1])

Try it online!

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

Java (JDK), 121 bytes

i->{int l=i.length()+2;return "#".repeat(l+2)+"\n#"+" ".repeat(l)+"#\n# "+i+" #\n#"+" ".repeat(l)+"#\n"+"#".repeat(l+2);}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Can omit the space after return \$\endgroup\$
    – ceilingcat
    Commented Jan 16, 2023 at 15:41
0
\$\begingroup\$

Vyxal j, 112 bitsv2, 14 bytes

L⇧‛# •½pøĊ\#vø.∞

Try it Online!

I know I said I love parallel apply, but I didn't actually need it here lol.

Explained

L⇧‛# •½pøĊ\#vø.∞­⁡​‎‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢⁣‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁣⁡‏⁠‎⁡⁠⁣⁢‏‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁣⁣‏⁠‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏⁠‎⁡⁠⁤⁢‏⁠‎⁡⁠⁤⁣‏‏​⁡⁠⁡‌⁢⁣​‎‎⁡⁠⁤⁤‏‏​⁡⁠⁡‌⁢⁤​‎‏​⁢⁠⁡‌­
  ‛# •            # ‎⁡Repeat each character in the string "# "
L⇧                # ‎⁢(len input) + 2 times
      ½           # ‎⁣Split into two halves - a string of #s and spaces
       p          # ‎⁤Prepend the input
        øĊ        # ‎⁢⁡And horizontally centre so that the input has enough spacing on both sides
          \#vø.   # ‎⁢⁢Prepend and append "#" to each string
               ∞  # ‎⁢⁣And mirror it, not mirroring the centre item
# ‎⁢⁤The j flag joins on newlines
💎

Created with the help of Luminespire.

\$\endgroup\$
1
2

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.