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

# MAWP, 18 bytes

%|092M3W!;~[;~!;~]


Dion's solution.

Try it!

### Older solution(19 bytes):

%|0~[65W3M;;]65W3M;


Try it!

• 18 bytes 8dion8.github.io/MAWP/…
– Dion
Aug 12, 2020 at 5:39
• Wow, lotta work for a byte. Aug 12, 2020 at 5:45
• Thats codegolf for ya :D
– Dion
Aug 12, 2020 at 5:46
• Argh! So close! Pushing a ! character in 1+ is already 11 bytes anyway...
– null
Aug 12, 2020 at 6:21

# Hexagony, 11 bytes

$>P...@1;,<  Try it online! $ > P
. . . @
1 ; , < .
. . . .
. . .

• P1 in place of 33 so that it resets the memory.
• MP is stationary

# (old) Hexagony, 17 bytes

{33...@;",<./{;/>


Try it online!

  { 3 3
. . . @
; " , < .
/ { ; /
> . .


{33 Puts an ! in left memory

;" Prints ! at the start and sets back mem

, Gets the next byte of input, < sends the IP U-R if null (which terminates it with @), D-R else

/{;/ Reflects IP, prints current byte, and sets it to the next line

> Redirects the IP to the start of the loop

# Vyxal, 4 bytes

¤\!V

 ¤\!V - Full program
-------
¤    - Push empty string to the stack
\!  - Push exclamation mark to the stack, better than !
V - take implicit input and replace the empty
string with the exclamation point


For an empty input, it will produce !0! because it considers the empty input 0, but I don't believe this can be fixed since taking input plainly returns 0 for "".

Try it Online!

# AWK, 11 bytes

gsub(a,"!")


Try it online!

This is similar to some other entries. It changes a null string to ! globally, using $0 as the default target, then prints the result since no other action is defined. Using a works since it's an undefined variable and is shorter than using "" (which would do the same thing). # 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!

# 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. Aug 31, 2019 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!

# Pyth, 8 bytes

+\!s+R\!


Try it online!

     Code | Explanation
----------+-------------------------------------
+\!s+R\!  | Code
+\!s+R\!Q | with implicit variables filled
----------+-------------------------------------
R  Q | For each d in input, replace d with:
+R\!  |  d+"!"
s      | Join results on empty string
+\!       | Add "!" to beginning
| Print (implicit)


# C (gcc), 69 62 50 bytes

Saved 12 bytes, thanks to ErikF's suggestion:

f(char*s){for(printf("!");*s;)printf("%c!",*s++);}


Try it online!

• You can use the passed-in pointer to iterate through the string and put the first printf in the for loop to save 12 bytes: Try it online! Aug 18, 2019 at 23:47
• Great idea! I'll update it Aug 18, 2019 at 23:49
• 46 bytes Aug 20, 2019 at 20:31
• @ceilingcat that is a completely different method. Should be posted as its own answer. Aug 21, 2019 at 13:09

# J, 12 bytes

'!',,@,.&'!'


Try it online!

# Aheui (esotope), 61 bytes

붖다따삭바사빠맣삭붛
밙봆＠뫃선차희져썬뻐


Try it online!

Nice small aheui code.

# 05AB1E, 6 bytes

-1 thanks to Kevin Cruijssen. Takes input as a list of characters.

õ.ø'!ý

• Are you a bit rusty golfing in 05AB1E? ;p Aug 19, 2019 at 7:49
• @KevinCruijssen Perhaps (now that I think about it I kinda am) :(. But I never actually knew how to exploit € so I guess I'm fine. Aug 19, 2019 at 7:52
• Yeah, it's something new this version.. The regular map ε works the same as in the legacy, but the € keeps all values on the stack. So €D would duplicate each character for example, while still keeping a 1D list. Not sure who's idea that was (probably Adnan), but it's useful in some cases. :) In other cases it can be pretty annoying as well, though. Aug 19, 2019 at 7:55
• @KevinCruijssen Interesting, thanks for the info (ahem ahem I do see it's useful sometimes :P) Aug 19, 2019 at 7:57
• -1 byte for your approach: õ.ø'!ý (surrounding with an empty string before joining). Aug 19, 2019 at 7:58

# Whitespace, 79 bytes

[N
S S N
_Create_Label_LOOP][S S S N
_Push_0][S N
S _Duplicate_0][T   N
T   S _Read_STDIN_as_character][T   T   T   _Retrieve_input][S N
S _Duplicate_input][S S S T S S T   N
_Push_9_tab][T  S S T   _Subtract][N
T   S S N
_If_0_jump_to_Label_END][S S S T    S S S S T   N
_Push_33_!][T   N
S S _Print_as_character][T  N
S S _Print_as_character][N
S N
N
_Jump_to_Label_LOOP][N
S S S N
_Create_Label_END][S S S T  S S S S T   N
_Push_33_!][T   N
S S _Print_as_character]


Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Since Whitespace inputs one character at a time, the input should contain a trailing tab (\t) so it knows when to stop reading characters and the input is done.

Try it online (with raw spaces, tabs and new-lines only).

Explanation in pseudo-code:

Start LOOP:
Character c = STDIN as character
If(c == '\t'):
Call function END
Print '!'
Print c
Go to next iteration of loop

function END:
print '!'


# MathGolf, 6 bytes

É'!\'!


Try it online!

## Explanation

Nothing fancy here, just a loop

É        for each character do the next 3 operations
'!      push single character "!"
\     swap top elements
'!   push single character "!"


# Scala, 17 bytes

_.replace("","!")


Try it online!

-3b thanks to Jo King!
Thanks to Dr Y Wit for linking me to the fact that not enforcing typing is allowed, and for the working TIO link.

• ah, you're right; fixing this. Aug 19, 2019 at 8:18
• Something like this perhaps? I don't know Scala
– Jo King
Aug 19, 2019 at 8:21
• oh? it looks like it compiles like this. Now I'm really sad we can't do _.replace("","!"). Nice one anyway :) Aug 19, 2019 at 8:23
• Actually, your answer does not fully comply with the rules because it's neither a program nor a function but rather just code snippet. However if you post a function literal s=>s.replace("","!") that would be shorter and more correct. Try it online! Aug 19, 2019 at 10:14
• Well, if you declare function val with the type then you can bind a function literal _.replace("","!") to it. Check my TIO link. Aug 19, 2019 at 12:19

# Emacs Lisp, 41 bytes

(while(or(insert"!")(not(forward-char))))


Assuming input is received as the contents of the current buffer

# Python 3, 26 bytes

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


Try it online!

• This exact answer has already been posted by Waterbyte Aug 20, 2019 at 10:06

# PHP, 75 bytes

Full program.

foreach(file('php://stdin')as$e)echo'!'.implode("!",str_split($e));echo"!";


Try it online!

# PHP, 100 bytes

foreach(explode("\n",file_get_contents('php://stdin'))as$e)echo'!'.implode("!",str_split($e))."!\n";


Try it online!

# PHP, 117 bytes

foreach(explode("\n",file_get_contents('php://stdin'))as$e){echo'!';foreach(str_split($e)as$c)echo$c."!";echo "\n";}


Try it online!

• Instead of echo'!'.implode("!",str_split($e));echo"!";, you can do echo'!',join("!",str_split($e)),"!";, which should save 7 bytes (read about join() on php.net/manual/en/function.join.php). According to php.net/manual/en/features.commandline.io-streams.php you can just use the constant STDIN (I don't know how if will work). Aug 20, 2019 at 10:00
• I did that intentionally , for saving bytes i removed brackets and i want to echo "!" after the foreach loop. Aug 20, 2019 at 10:07
• Oh, yeah, you're right. I missed that. But if you are reading line by line, you should output the newlines as well, except for the last one. echo doesn't output newlines, so, everything should be in the same line. You can use echo'!',join('!',str_split(file_get_contents('php://stdin'))),'!';, which is only 66 bytes long (or less, if the constant STDIN works) and fixes that. Aug 20, 2019 at 10:11
• I mean, echo doesn't automatically outputs newlines at the end, so, if you read line by line, you have to output the newline, if it isn't in the input. Aug 20, 2019 at 10:22
• file() returns an array of strings , each string has a newline as last character . @IsmaelMiguel Aug 20, 2019 at 10:31

"$args"-replace"","!"  Try it online! Boring regex replacement is boring. ;-) # Kotlin, 50 bytes fun f(s:String)=s.map{"!$it"}.joinToString("")+"!"


Try it online!

This is my first participation on this site, thought I would try a challenge with my new favourite language.

• 29 bytes with replace and a lambda Sep 11, 2019 at 14:59

# Brain-Flak, 54 bytes

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


Try it online!

Code:

(())                            push 1, so the loop runs at least one time
{                               while stack 1 isn't empty
({}<>)                      move letter to stack 2
((((()()()()){}){}){}())    push 33 (Exclamation Mark) on stack 2
<>                          return to stack 1 for checking if the end is reached
}
<>{({}<>)<>}                    move everything from stack 2 back to stack 1 (otherwise the text would be reversed)
<>{}                            switch to stack 1 and delete leading 1


# Forth (gforth), 40 39 bytes

: f ." !"bounds do i 1 type ." !"loop ;


Try it online!

### Code Explanation

: f           \ start a new word definition
." !"bounds \ print ! and place the ending address on the stack
i         \ get address of current char
1 type    \ print the char at the given address
." !"     \ print !
loop        \ end the loop
;             \ end the word definition


# PHP (7.4), 31 bytes

Based on Petah's comment.

fn($s)=>preg_replace(__,'!',$s)


Try it online!

# PHP, 52 bytes

function($s){return'!'.join('!',str_split($s)).'!';}


Try it online!

• PHP 7.4 fn($s)=>preg_replace('//', '!',$s); Aug 20, 2019 at 0:06

# Keg, 9 8 bytes

?⑷\!⑸\!^


Using a mapping approach really does help

Try it online!

### 9 bytes

?^(\!')\!


Pretty much a port of the MathGolf submission.

Try it online!

• I think the explicit ? is not needed. Aug 20, 2019 at 11:06
• No. It isn't. Updated my answer Aug 20, 2019 at 11:07

# Pushy, 9 bytes

L:33{;33"


Try it online!

L:   ;        \ len(input) times do:
33          \    Append character 33 (exclamation mark)
{         \    Cyclically shift the string left once
33      \ Append character 33 again
"     \ Print the result


# Javascript (27 bytes)

My first code golf, 27 bytes in Javascript. Could probably shave some more bytes off, but I'm not too sure.

f={x:s=>!\${s.join('!')}!}


Example Usage

console.log(f.x('1 2 3 4 5'.split('')))
// Output: !1! !2! !3! !4! !5!

• Welcome to CCGC! Using the way of defining lambdas that has been kind of standardized on this site, this is 21 bytes: Try it online! (unless the lambda is called recursively, the f= does not need to be counted) Dec 25, 2019 at 3:58

# 1+, 20 19 bytes

1##11"+""*"**+;,;1#


1+ 19 vs 18 MAWP

Error-terminating because comparison is expensive.

• I think i'm winning for now ;) btw, mawp submissions should be here github.com/8Dion8/8Dion8.github.io/blob/master/scraper/…
– Dion
Aug 12, 2020 at 8:20
• @Dion Yeah, only 1 byte away! I'm creating the 1+ answer list now.
– null
Aug 12, 2020 at 8:32
• I created a python script to gather everything... Maybe you could change it a bit and use it for gathering 1+ answers? If you are interested, ill clean up the code so its understandable xd
– Dion
Aug 12, 2020 at 8:51
• @Dion Apparently your script searches for 1+ with the SE search tool but that will not work well for 1+. Manual 1+ answer list
– null
Aug 12, 2020 at 10:58
• github.com/8Dion8/8Dion8.github.io/blob/master/scraper/… is the current script
– Dion
Aug 12, 2020 at 11:12

# K (ngn/k), 13 12 bytes

-1 byte from @ngn's savings

{x,/y,'x}"!"


Try it online!

Creates a function projection, with x fixed to "!".

• y,'x pair-wise append an exclamation mark to each character in the input
• x,/ flatten the list, using "!" as the seed (to prepend the leading "!")
• save a byte by turning "!" into a curried argument: f:{x,/y,'x}"!"
– ngn
Feb 12, 2021 at 19:05

# Charcoal, 6 bytes

⭆Ｓ⁺!ι!


Try it online! Link is to verbose version of code. Works on empty strings too. Input format is somewhat cumbersome due to Charcoal's lack of EOF. Works by preceding each character with a ! and then suffixing a final ! on the end. Alternative version, also 6 bytes:

⪫!!⪫Ｓ!


Try it online! Link is to verbose version of code. Works by joining the characters on ! and then wrapping them in !!.