!I!n!s!e!r!t! !i!n!b!e!t!w!e!e!n!

Posted from here.

This challenge is highly "distilled" from this question. Special thanks to @Akababa!

In this task, you should insert an exclamation mark at the start of the string and after every character.

Rules

• There will always be a non-empty-string input. The input will not contain tabs either. You can assume that the input only contain non-extended ASCII printable characters and newlines.
• The input will not contain trailing newlines as long as your language can't detect a newline.
• This is a contest; the shortest answer should win.

Examples

• 4 newlines result in 5 newline-delimited exclamation marks. It is very hard to put this as a Markdown text, so this is stated instead.
1 2 3 4 5 6
129591 129012 129127 129582

0


Outputs

!1! !2! !3! !4! !5! !6!
!1!2!9!5!9!1! !1!2!9!0!1!2! !1!2!9!1!2!7! !1!2!9!5!8!2!
!
!0!

asd afjoK ak:e
kPrLd
fOJOE;
KFO
KFkepjgop sgpaoj   faj


Outputs

!a!s!d! !a!f!j!o!K! !a!k!:!e!
!k!P!r!L!d!
! ! ! ! !f!O!J!O!E!;!
! ! ! ! !K!F!O!
!K!F!k!e!p!j!g!o!p! !s!g!p!a!o!j! ! ! !f!a!j!


A base test case with only one character:

a


Outputs

!a!


(Auto-completion! Just kidding, there is no such thing.) Contains exclamation marks:

!!
!!
!!
!!
!!


Outputs:

!!!!!
!!!!!
!!!!!
!!!!!
!!!!!

• very similar question Aug 18, 2019 at 11:06
• I really don't understand the downvote - this is a clear and well written challenge. Re: being a duplicate - it's not (preceding '!' makes for a big difference), and I don't believe anyone has suggested so (no close votes). Aug 18, 2019 at 13:45
• if a language can't tell the difference between a\n and a, can we require that there are no trailing newlines? Aug 18, 2019 at 14:29
• Downvotes are inserted between every upvote, just like what the challege describes.
– user85052
Aug 18, 2019 at 14:34
• Is the case of a single space input " ", is the output supposed to be "!" or "! !"
– Kai
Aug 19, 2019 at 3:52

Thanks to A__ for halving the byte count!

!


Try it online!

Replaces nothing with !

Python 3, 26 bytes

lambda s:s.replace('','!')


Try it online!

• Welcome to the site! Aug 19, 2019 at 12:37

Retina 0.8.2, 2 bytes


!


Try it online! At last, a challenge where Retina has a built-in!

• Actually, I created this challenge based on this Retina built-in.
– user85052
Aug 18, 2019 at 11:04
Aug 18, 2019 at 15:01
– user85052
Aug 18, 2019 at 15:19
• @A__ Right, I forgot about that feature (If there is only one non-function line…). You may want to re-consider your check-mark.
Aug 18, 2019 at 19:02

Python 3, 27 bytes

lambda s:f"!{'!'.join(s)}!"


Try it online!

Honestly, I hope someone can show me a cool way to do this with a smaller byte count.

• This doesnt handle the empty line case correctly Aug 19, 2019 at 4:22
• @flakes What do you mean? If you mean an empty string: we do not need to handle an empty string (and regardless this outputs !! in that case, which is what makes sense to me). If you mean the string \n: it does, since the correct output is !\n!. Aug 19, 2019 at 9:08
• @JAD As far as I can see, it doesn't have an empty string in the examples. Not only that, but the first rule literally states "there will always be a non-empty string input." Aug 19, 2019 at 11:13
• Ah I was incorrect. The first example has an empty line in the middle of the input. But this answer will handle placing the exclamation point in the middle of that, !\n!\n!. nice work. Aug 19, 2019 at 13:18
• codegolf.stackexchange.com/questions/190223/insert-nbetween/… as you were saying to show a shorter way Aug 20, 2019 at 7:14

Python 2, 27 bytes

lambda s:'!%s!'%'!'.join(s)


Try it online!

('!':).(>>=(:"!"))


-1 byte thanks to @nimi

Try it online!

• ('!':). saves a byte.
– nimi
Aug 18, 2019 at 15:09

brainfuck, 24 22 bytes

-2 bytes thanks to JoKing.

-[-[-<]>>+<],[>.<.,]>.


Try it online!

• damn, that's badass Aug 19, 2019 at 20:20

Labyrinth,  19 11 10  9 bytes

33
..
",@


Try it online!

How?

We enter the Labyrinth at the top-left facing right with an infinite stack of zeros...

                         I / O    stack
0,0,0,...
3 - pop * 10 + 3                  3,0,0,0,...
- 2 neighbours, forward
3 - pop * 10 + 3                  33,0,0,0,...
- 2 neighbours, forward
. - pop & print chr          !    0,0,0,...
- T junction from the side
-   TOS==0, forward
, - read chr or -1       L        76,0,0,0,...   or   -1,0,0,0
- T junction from the base
-   if TOS > 0 right:
" -     no-op                     76,0,0,0,...
-     2 neighbours, forward
. -     pop & print chr      L    0,0,0,...
-     T junction from the side
-       TOS==0, forward
3 -       ...back to the start
-   elif TOS == -1 left:
@ -     exit                                          we're out!

* right, but on the first occasion (from above) we hit the wall and turn
around, so that's like a left


Luckily we don't need to handle un-printables, otherwise the first zero-byte would turn us around at , and play havoc.

JavaScript (ES6), 19 bytes

Takes input as an array of characters.

s=>!${s.join!}!  Try it online! JavaScript (ES6), 23 20 bytes Saved 3 bytes thanks to @ShieruAsakoto Takes input as a string. s=>[,...s,,].join!  Try it online! JavaScript (ES6), 22 bytes Suggested by @tjjfvi Takes input as a string. s=>s.replace(/|/g,"!")  Try it online! • Alternative with .replace, 22 bytes Aug 18, 2019 at 19:12 • @tjjfvi Nifty one! Aug 18, 2019 at 19:18 • I've got a 20 for your 23: s=>[,...s,,].join!  Aug 19, 2019 at 13:42 Pepe, 47 bytes REREEeRErEErREeeEeeeeEREEeeREEeereeREEEEeeEReee  Try it online! Explanation: REREEeRE # Push 0,then input (str),then 0 -> (R) # The zeroes are pushed to correct the inserting rEE # Begin loop labelled 0 -> (r) rREeeEeeeeE # Push "!" -> (R) # r flag inserts it instead of pushing REEeeREEee # Move pointer pos 2 steps forward -> (R) ree # Loop while (R) != 0 REEEEeeE # Remove characters of (R) that are in stack of (r) # Removes the 0 in (R) Reee # Output (R)  • How do you write this code? Is there some code converter that you use? This seems crazy to try and write Aug 19, 2019 at 20:21 • @Cruncher 1) I use this as my guide. 2) Nope, I don't use a code converter, I just use the guide to write the code. Aug 20, 2019 at 12:59 R, 25 bytes function(x)gsub("","!",x)  Try it online! A function accepting and returning a character vector. • Can shave 3 bytes by switching the function form to scan(,''), like so tio.run/##K/r/P724NElDSUlHSVFJpzg5MU9DR11dU/O/… Aug 19, 2019 at 13:37 • @Sumner18 thanks. I started with that but it splits input at spaces. Aug 19, 2019 at 14:54 • @Sumner18 The challenge asks to handle input with newlines, which can't be done with scan (but which Nick's solution does handle, at least if you display the output with cat.) Aug 19, 2019 at 21:14 8086 machine code, .COM format (MS-DOS 2+), 32 bytes (-1 depending on emulator: see below) For best results redirect standard input from a file, as typing gives odd-looking output due to no buffering; also, newlines look a little weird because they are stored as CR LF, and the CR part messes up the output. This program behaves fine in an actual MS-DOS emulation (e.g. PCjs) but DOSBox seemed to have issues with Ctrl+Z EOF (see comments in the assembly listing), so DON'T try to enter input using the console in DOSBox unless you add the extra check! BB 01 00 53 59 BA 0B 01 B4 40 CD 21 4A 4B B4 3F CD 21 85 C0 74 09 B4 40 43 41 CD 21 49 EB EE C3  Some interesting bits: • I saved some data space by reusing memory that had already been executed (the 21H in INT 21H happens to be !) • I was almost able to use an interesting trick that I found on the page "The Hidden Power of BCD Instructions" which would have allowed me to use AAA instead of a standard TEST to compare AL to 0, saving one byte. Unfortunately, this is not fully documented so I couldn't rely on it: for example, PCjs doesn't adjust anything but the carry and auxiliary carry flags. :-( Assembly code (TASM ideal mode): IDEAL MODEL TINY CODESEG ORG 100H ;; DOSBox (tested with 0.74-2) didn't seem to handle Ctrl-Z as EOF ;; so uncomment the ";;" lines to run it there. MAIN: MOV BX,1 PUSH BX POP CX MOV DX,OFFSET MAIN_1+1 ; The 21H in INT 21H MOV AH,40H MAIN_1: INT 21H DEC DX ;;PUSH DX ;;POP SI IO_LOOP: DEC BX MOV AH,3FH INT 21H ;;; This should work on an non-emulated PC. ;;;AAA ; AL=0? TEST AX,AX JZ DONE ;;CMP [BYTE PTR SI],1AH ;;JZ DONE MOV AH,40H INC BX INC CX INT 21H DEC CX JMP IO_LOOP DONE: RET ENDS END MAIN  6502, 12 bytes (13 bytes if Apple II) 6502 The machine code assumes that a pair of zero page locations are connected to character input ($FE) and output (FF) hardware. Many 6502-based systems facilitate I/O in this fashion, albeit I/O address are usually not in zero page.

For simplicity, I used Py65, a 6502 microcomputer system simulator written in Python.

Here is a memory dump from Py65. You can load the following code anywhere in zero page such that it does not overlap $FE and$FF.

       PC  AC XR YR SP NV-BDIZC
6502: 0000 00 00 00 ff 00110010
.mem 0:b
0000:  a9  21  85  ff  a5  fe  f0  fc  85  ff  d0  f4


Running in a Windows command window, you can paste (Ctrl+V) any text you desire, or you can simply type. If typing, press Ctrl+J for a newline (same ASCII char). Press Ctrl+C to interrupt the processor and return to the Py65 command prompt.

Naturally, assembly code is easier to read.

       PC  AC XR YR SP NV-BDIZC
6502: 0000 00 00 00 ff 00110010
.d 00:0b
$0000 a9 21 LDA #$21
$0002 85 ff STA$ff
$0004 a5 fe LDA$fe
$0006 f0 fc BEQ$0004
$0008 85 ff STA$ff
$000a d0 f4 BNE$0000


For clarity, here is the assembly code in CBA65 format.

; ASSEMBLE:
; cba65 bangit
;
; python3 py65/monitor.py -i 00fe -o 00ff -l bangit.bin
; goto 0000

.FILES  BIN=256

; I/O LOCATIONS
GETC    .EQU    $FE ; (1) MOVING PY65'S GETC TO ZP SHAVES 1 BYTE PUTC .EQU$FF             ; (1) MOVING PY65'S PUTC TO ZP SHAVES 2 BYTES

.ORG    $0000 VROOM LDA #'!' STA PUTC VROOM2 LDA GETC BEQ VROOM2 STA PUTC BNE VROOM .END  Apple II The code above assumes a null indicates there is no input, so continues polling until a non-null value is returned. For comparison, the Apple I and Apple II signals availability of a new character by setting bit 7 of the keyboard I/O address, which then needs to be cleared after fetching the character. On those systems, character I/O usually is performed by calling system monitor routines instead of accessing the hardware directly. By calling RDKEY ($FD0C) and COUT ($FDED), the Apple II equivalent of the above can be coded in 13 bytes, and is runnable anywhere in RAM. Here is the code I ran in an Apple //e emulator, a2ix on Android 9. Pressing Return has the same effect as a newline. *300L 0300- A9 A1 LDA #$A1
0302-   20 ED FD    JSR   $FDED 0305- 20 0C FD JSR$FD0C
0308-   20 ED FD    JSR   $FDED 030B- F0 F3 BEQ$0300


Did you notice that instead of the normal ASCII value #$21 for the exclamation point, #$A1 is used instead? That's because sending standard ASCII values to COUT causes them to be displayed in "inverse mode," black on white. Displaying ASCII in normal white on black requires adding #$80 to the character value in the accumulator before calling COUT. Because RDKEY returns characters with the hi-bit set, assembly programs generally cleared the bit of the character to obtain its ASCII value before using it. • Welcome to the site! :) Aug 21, 2019 at 9:05 • Thank you, @Rahul! – lee Aug 21, 2019 at 10:55 sed, 12 bytes s/\b\|\B/!/g  Try it online! -3 bytes thanks to Cows Quack • Actually the Sed code is just 15 characters there: Try it online!. Aug 19, 2019 at 15:11 • Great, thank you. I was unclear how that worked... Aug 19, 2019 at 15:13 • s/\b\|\B/!/g also works for 12 bytes Aug 20, 2019 at 7:15 • @Cowsquack thank you. updated. Aug 20, 2019 at 12:47 Jelly, 5 bytes Ż”!ṁż  A full program accepting a string, which prints the result. Try it online! How? Ż”!ṁż - Main Link: list of characters, s e.g. "abc" ”! - character '!' '!' ṁ - mould like: Ż - s with a zero prepended "!!!!" ż - zip together with s ["!a","!b","!c",'!'] - implicit (smashing) print !a!b!c!  Befunge-98 (PyFunge), 7 bytes '!,#@~,  Try it online! Perl 5 -p0, 17 6 bytes s,,!,g  Try it online! My original answer was -p and $_='!'.s,.,$&!,gr. Thanks to @Nahuel Fouilleul for cutting 11 bytes and to @Grimy for the -p0 tip. • 6 bytes Aug 19, 2019 at 8:32 • @NahuelFouilleul -lp gives incorrect output for the \n\n\n\n test case (returns 4 newline-separated ! instead of the specified 5). -p0 works correctly. Aug 19, 2019 at 11:20 MarioLANG, 95949089 69 bytes ++++++ ======< >) >+++++++",+[ =======<.==< >+++++++!(.- ========#===  Try it online! First time trying out MarioLANG, that was a lot of fun! Thanks to Jo King for -20 bytes Explanation: So, as the name implies, MarioLANG is made to execute like a game of Super Mario Bros. It operates similarly to BF, with memory arranged in a tape of cells. There are operators to increment, decrement, print (as ascii or numeric) and read into the current memory cell, and operators to move left or right along the tape. Mario (the instruction pointer) always begins in the top left cell of the program, with his intended direction of motion set to the right. If Mario does not have a floor-like object beneath him (=, ", or #), he will fall until he reaches a floor-like object. If Mario leaves the program space, the program ends due to Game Over :( This specific program can basically be split into two halves: the setup, and the loop.  Setup Loop ----------------------------------------------- | ++++++ | ======< | >) >+++++++ | ",+[ =======< | .==< >+++++++ | !(.- ======== | #===  In the Setup section, we're simply incrementing the first memory cell until we reach 33 - the ASCII value for "!". Easy enough; if this can be golfed, it's purely a matter of shape. Mario starts from the top left, picks up 10 coins, starts falling when picking up the 11th, switches directions, then repeats. He picks up the last 11 coins without switching directions; he ends the setup section at the bottom-rightmost "+". In the loop section, Mario starts by reaching an elevator. The "!" operator makes him cease motion, so that he remains on the elevator. On the way up, it prints the corresponding ASCII character to the current memory cell's value (this one is always 33, "!"), then switches to the next cell in memory. Mario reaches the top and sets his direction to the right. He falls, and reads a character from input as its ASCII value (or -1 if no character). We increment because the only measure of control in MarioLANG is to skip an instruction if the current memory cell has a value of 0. If it does, we skip changing Mario's direction, so he will walk right off of the next floor to his doom. If it does not, we set direction to left; walking left off of the floor below decrements the current cell back to its previous value, that value is printed, and we move back to the first memory cell before getting back on the elevator. Previous version (89 bytes): +++++++++++>, ==========@"+ +++++++++++)[ @==========.==< +++++++++++!(.- ===========#===  • 62 bytes by using a multiplication loop instead of just a counter – Jo King Aug 21, 2019 at 12:35 • Okay now THAT is cool. I'll update as soon as I have time to redo the explanation, thanks a bunch! Aug 21, 2019 at 12:41 • Aha! 60 bytes by multiplying 5*6 + 3 instead of 8*4+1 – Jo King Aug 21, 2019 at 12:43 • Man, I know it isn't exactly your first rodeo, but this is really impressive. xD Aug 21, 2019 at 12:45 • Actually, this is my first time golfing MarioLANG. I just have some experience with brainfuck as well as other 2D languages – Jo King Aug 21, 2019 at 12:46 Perl 6, 16 11 bytes {S:g/<(/!/}  Try it online! Replaces all zero width matches with exclamation marks. Null regexes are not allowed, so we use a capture marker to capture nothing instead Zsh, 32 23 bytes <<<!${(j:!:)${(s::)1}}!  (s::) splits into characters, (j:!:) joins on !s. C# (Visual C# Interactive Compiler), 28 bytes s=>$"!{String.Join("!",s)}!"


Try it online!

• nice use of interpolated strings, wouldn't have thought of that Aug 20, 2019 at 4:12

Java 8, 20 bytes

A lambda function from String to String.

s->s.replace("","!")


Try It Online

05AB1E, 4 bytes

€'!Ć


I/O as a list of characters.

Try it online.

Explanation:

€'!  '# Prepend a "!"-item before each character in the (implicit) input-list
Ć  # Enclose (append the first character of the list at the end of it)
# (after which the result is output implicitly)


Triangular, 15 13 bytes

B\3;#*~.,</@<


Try it online!

-2 bytes after remembering that Triangular has a conditional halt operator.

I believe this is as short as it gets on this one. Triangular does have conditional direction-change operators, but they unfortunately work differently than the other conditionals. While all others check if ToS <= 0, the direction-changing conditionals check ToS != 0. If this weren't the case, we would have 10 bytes in the form of Bq3~#*/@<<.

Ungolfed:

    B
\ 3
; # *
~ . , <
/ @ <
----------------------------------------------------
B3*              - Push 11 and 3, then pop both and push their product.
<,<          - Change directions 3 times (to save 2 bytes on last line)
@/       - Print Top of Stack value as a character, do not pop
~;\    - Push a character from input to ToS. Halt if ToS <= 0. Change Direction.
#  - Print ToS as a character and pop


Previous Version (15 bytes):

B.3\.*#).(/?~@<


Wolfram Language (Mathematica), 26 bytes

Riffle[0#~Join~{0}+"!",#]&


Try it online!

Takes and returns a list of characters.

><>, 11 6 bytes

"!"oio


Try it online!

Saved 5 bytes thanks to Jo King, suggesting exiting with an error. Previous version which does not exit with an error:

"!"oi:0(?;o


Try it online!

• You can remove the :0(?; to terminate in an error
– Jo King
Aug 19, 2019 at 10:30

SimpleTemplate, 23 bytes

This is a language I wrote, and it was supposed to be for templates, but well.

!{@eachargv.0}{@echo_}!


Should be almost self-explanatory, once you see the ungolfed code:

!{@each argv.0 as char} {@echo char}!{@/}


And an explanation:

• ! - Prints the literal ! character
• {@each argv.0 as char} - Loops through every character, with the value set to the variable char (optional, the default variable is _).
argv.0 is the first parameter passed to the render() method of the compiler.
• {@echo char}! - outputs the char variable and a literal ! character.
For the golfed version, the default variable _ is used instead.
• {@/} - closes the loop (optional)

Pure SimpleTemplate solution:

{@fn x}!{@eachargv.0}{@echo_}!{@/}{@/}


Creates a function x that outputs the same result.

You can use it like this:

{@call x "this is an example"}


You can try all of this on: http://sandbox.onlinephpfunctions.com/code/f6baff8d411fc8227ece81eccf05b6e7d3586bfa

On the line 908, you can use the variables $golfed, $ungolfed and $fn to test all the versions. However, if it is allowed to use a character array, the code is simplified (20 bytes): !{@echoj"!" argv.0}!  And ungolfed: !{@echo separator "!" argv.0}!  Basically, outputs all items in the array, joined by "!", surrounded by literal !. Due to limitations in the compiler class, the space is mandatory (in the golfed version). This code is also extremelly harder to use in pure SimpleTemplate (using the function as example): {@fn x}!{@echoj"!" argv.0}!{@/} {@// alternative: @call str_split into a "a char array"} {@set a "a", " ", "c", "h", "a", "r", " ", "a", "r", "r", "a", "y"} {@call x a}  The @call can call a function that exists in PHP, which means that it isn't a pure SimpleTemplate solution. Bash, 36 bytes while read -n1 c;do printf \!$c;done


Try it online!

This counts on the newline terminating the input for the last ! mark.

• Welcome! Please consider adding an explanation or link to an interpreter or something, because code-only answers are automatically flagged as low-quality. Aug 19, 2019 at 22:29
• @mbomb007, thanks for the pointer. Aug 19, 2019 at 22:42
• Unfortunately this does not add an ! at the end of the input. Aug 20, 2019 at 7:18
• @Cowsquack: on my terminal, the newline that terminates the input gets the ! added. On tio.run, the input needs to be terminated with a carriage return. I've updated the link to the Try it Online to reflect that. Aug 20, 2019 at 16:17

Ruby, 17 16 bytes

->s{s.gsub'',?!}


Try it online!

Thanks Value Ink for -1 byte

• Remove the space after gsub. Aug 20, 2019 at 19:05

VBA, 75 bytes 72 bytes

72 bytes as a sub that outputs to the immediate window (thanks @taylorscott)

Sub s(x):Debug.?"!";:For i=1To Len(x):Debug.?Mid(x,i,1)"!";:Next:End Sub


75 bytes as a function that returns the formatted string.

Function t(x):t="!":For i=1To Len(x):t=t &Mid(x,i,1) &"!":Next:End Function


Which expands to and is readable as

Public Function t(x)
t = "!"
For i = 1 To Len(x)
t = t & Mid(x, i, 1) & "!"
Next
End Function


Test cases

Public Sub test_golf()

x = "1 2 3 4 5 6" & vbCr & "129591 129012 129127 129582" & vbCr & vbCr & "0"
'x = "a"
'x = "!!" & vbCr & "!!" & vbCr & "!!" & vbCr & "!!" & vbCr & "!!"

Debug.Print x
s(x)              ' to call the sub
Debug.Print t(x)  ' to call the function

End Sub

• You can get this down to a 48 byte immediate window function as ?"!";:For i=1To[Len(A1)]:?Mid([A1],i,1)"!";:Next Aug 25, 2019 at 18:25
• @TaylorScott, I agree that would work in Excel VBA. I was just thinking of a pure VBA solution.
– Ben
Aug 26, 2019 at 0:58
• If you want to keep it as a pure VBA function, you can still get it down a bit by switching it to a sub and printing directly to the console as Sub t(x):Debug.?"!";:For i=1To Len(x):Debug.?Mid(x,i,1)"!";:Next:End Sub or Sub t(x):s="!":For i=1To Len(x):s=s+Mid(x,i,1)+"!":Next:Debug.?s:End Sub Sep 11, 2019 at 20:48