# !I!n!s!e!r!t! !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 – Giuseppe Aug 18 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). – Jonathan Allan Aug 18 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? – Giuseppe Aug 18 at 14:29
• Downvotes are inserted between every upvote, just like what the challege describes. – A̲̲ Aug 18 at 14:34
• This is dead. – V. Courtois Aug 19 at 7:30

Thanks to A__ for halving the byte count!

!


Try it online!

Replaces nothing with !

# 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 – flakes Aug 19 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!. – Jonathan Allan Aug 19 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." – Reinstate Monica Aug 19 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. – flakes Aug 19 at 13:18
• codegolf.stackexchange.com/questions/190223/insert-nbetween/… as you were saying to show a shorter way – U10-Forward - Reinstate Monica Aug 20 at 7:14

# Python 3, 26 bytes

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


Try it online!

# 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. – A̲̲ Aug 18 at 11:04
• -1 bytes in QuadR. – A̲̲ Aug 18 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. – Adám Aug 18 at 19:02

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


-1 byte thanks to @nimi

Try it online!

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

# Python 2, 27 bytes

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


Try it online!

# JavaScript (ES6), 19 bytes

Takes input as an array of characters.

# 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 – Nahuel Fouilleul Aug 19 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. – Grimmy Aug 19 at 11:20

# 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 ; ; LOAD/RUN ; 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! :) – Rahul Bharadwaj Aug 21 at 9:05
• Thank you, @Rahul! – lee Aug 21 at 10:55

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. – mbomb007 Aug 19 at 22:29 • @mbomb007, thanks for the pointer. – spuck Aug 19 at 22:42 • Unfortunately this does not add an ! at the end of the input. – Kritixi Lithos Aug 20 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. – spuck Aug 20 at 16:17 # 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 at 12:35 • Okay now THAT is cool. I'll update as soon as I have time to redo the explanation, thanks a bunch! – Reinstate Monica Aug 21 at 12:41 • Aha! 60 bytes by multiplying 5*6 + 3 instead of 8*4+1 – Jo King Aug 21 at 12:43 • Man, I know it isn't exactly your first rodeo, but this is really impressive. xD – Reinstate Monica Aug 21 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 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 # 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 – Zac Faragher Aug 20 at 4:12

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


# Befunge-98 (PyFunge), 7 bytes

'!,#@~,


Try it online!

# 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\.*#).(/?~@<


# 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. # Ruby, 17 16 bytes ->s{s.gsub'',?!}  Try it online! Thanks Value Ink for -1 byte • Remove the space after gsub. – Value Ink Aug 20 at 19:05 # Gema, 11 characters \A=\! ?=?\!  Unfortunately ! starts a comment in Gema, so must be escaped. Sample run: bash-5.0$ echo -ne '1 2 3 4 5 6\n129591 129012 129127 129582\n\n0' | gema '\A=\!;?=?\!'
!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!


Try it online!

# 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!


# Japt, 4 bytes

rP'!


Try it

r      Replace
P     all empty strings "" with
'!   An exclamation mark (If '!' wasn't a special character in Japt I could remove the quote)

• You'd only be able to ditch the ' if the replacement were a number. – Shaggy Aug 31 at 16:05

# Japt, 4 bytes

rP'!


Try it

# Japt-P, 7 bytes

Unfortunately ! is a reserved character, necessitating the quotation marks.

ï'! i'!


Try it

Not much to explain: ï is Cartesian product and i prepends.

# Perl 5 + -p, 11 bytes

s/^|.\K/!/g


Try it online!