24
\$\begingroup\$

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:

!!!!!
!!!!!
!!!!!
!!!!!
!!!!!
\$\endgroup\$
  • 6
    \$\begingroup\$ very similar question \$\endgroup\$ – Giuseppe Aug 18 at 11:06
  • 7
    \$\begingroup\$ 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). \$\endgroup\$ – Jonathan Allan Aug 18 at 13:45
  • 1
    \$\begingroup\$ if a language can't tell the difference between a\n and a, can we require that there are no trailing newlines? \$\endgroup\$ – Giuseppe Aug 18 at 14:29
  • 13
    \$\begingroup\$ Downvotes are inserted between every upvote, just like what the challege describes. \$\endgroup\$ – A _ Aug 18 at 14:34
  • 2
    \$\begingroup\$ This is dead. \$\endgroup\$ – V. Courtois Aug 19 at 7:30

64 Answers 64

2
\$\begingroup\$

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

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!

\$\endgroup\$
  • 1
    \$\begingroup\$ 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! \$\endgroup\$ – ErikF Aug 18 at 23:47
  • \$\begingroup\$ Great idea! I'll update it \$\endgroup\$ – Pandazoic Aug 18 at 23:49
  • \$\begingroup\$ 46 bytes \$\endgroup\$ – ceilingcat Aug 20 at 20:31
  • \$\begingroup\$ @ceilingcat that is a completely different method. Should be posted as its own answer. \$\endgroup\$ – Potato44 Aug 21 at 13:09
2
\$\begingroup\$

Java 8, 20 bytes

A lambda function from String to String.

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

Try It Online

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

J, 12 bytes

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

Try it online!

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

Aheui (esotope), 61 bytes

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

Try it online!

Nice small aheui code.

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

05AB1E, 6 bytes

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

õ.ø'!ý

Try it online! or Try it online! (with input as a string)

\$\endgroup\$
  • 1
    \$\begingroup\$ Are you a bit rusty golfing in 05AB1E? ;p \$\endgroup\$ – Kevin Cruijssen Aug 19 at 7:49
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – Mr. Xcoder Aug 19 at 7:52
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Kevin Cruijssen Aug 19 at 7:55
  • 1
    \$\begingroup\$ @KevinCruijssen Interesting, thanks for the info (ahem ahem I do see it's useful sometimes :P) \$\endgroup\$ – Mr. Xcoder Aug 19 at 7:57
  • \$\begingroup\$ -1 byte for your approach: õ.ø'!ý (surrounding with an empty string before joining). \$\endgroup\$ – Kevin Cruijssen Aug 19 at 7:58
2
\$\begingroup\$

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

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

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.

\$\endgroup\$
  • \$\begingroup\$ ah, you're right; fixing this. \$\endgroup\$ – V. Courtois Aug 19 at 8:18
  • \$\begingroup\$ Something like this perhaps? I don't know Scala \$\endgroup\$ – Jo King Aug 19 at 8:21
  • \$\begingroup\$ oh? it looks like it compiles like this. Now I'm really sad we can't do _.replace("","!"). Nice one anyway :) \$\endgroup\$ – V. Courtois Aug 19 at 8:23
  • \$\begingroup\$ 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! \$\endgroup\$ – Dr Y Wit Aug 19 at 10:14
  • 1
    \$\begingroup\$ Well, if you declare function val with the type then you can bind a function literal _.replace("","!") to it. Check my TIO link. \$\endgroup\$ – Dr Y Wit Aug 19 at 12:19
2
\$\begingroup\$

Emacs Lisp, 41 bytes

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

Assuming input is received as the contents of the current buffer

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

Wolfram Language (Mathematica), 26 bytes

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

Try it online!

Takes and returns a list of characters.

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

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

\$\endgroup\$
  • 2
    \$\begingroup\$ You can remove the :0(?; to terminate in an error \$\endgroup\$ – Jo King Aug 19 at 10:30
2
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ 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). \$\endgroup\$ – Ismael Miguel Aug 20 at 10:00
  • \$\begingroup\$ I did that intentionally , for saving bytes i removed brackets and i want to echo "!" after the foreach loop. \$\endgroup\$ – Micio Informatico Aug 20 at 10:07
  • \$\begingroup\$ 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. \$\endgroup\$ – Ismael Miguel Aug 20 at 10:11
  • \$\begingroup\$ 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. \$\endgroup\$ – Ismael Miguel Aug 20 at 10:22
  • \$\begingroup\$ file() returns an array of strings , each string has a newline as last character . @IsmaelMiguel \$\endgroup\$ – Micio Informatico Aug 20 at 10:31
2
\$\begingroup\$

Keg, 9 bytes

?^(\!')\!

Pretty much a port of the MathGolf submission.

Try it online!


Keg, 8 bytes

^(\!')\!

Try it online!

\$\endgroup\$
  • \$\begingroup\$ I think the explicit ? is not needed. \$\endgroup\$ – manatwork Aug 20 at 11:06
  • \$\begingroup\$ No. It isn't. Updated my answer \$\endgroup\$ – Jono 2906 Aug 20 at 11:07
2
\$\begingroup\$

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.

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

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

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
  do          \ start a counted loop from start-addr to end-addr      
    i         \ get address of current char
    1 type    \ print the char at the given address
    ." !"     \ print !
  loop        \ end the loop
;             \ end the word definition
\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ You can get this down to a 48 byte immediate window function as ?"!";:For i=1To[Len(A1)]:?Mid([A1],i,1)"!";:Next \$\endgroup\$ – Taylor Scott Aug 25 at 18:25
  • 1
    \$\begingroup\$ @TaylorScott, I agree that would work in Excel VBA. I was just thinking of a pure VBA solution. \$\endgroup\$ – Ben Aug 26 at 0:58
  • \$\begingroup\$ 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 \$\endgroup\$ – Taylor Scott Sep 11 at 20:48
2
\$\begingroup\$

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!

\$\endgroup\$
  • 1
    \$\begingroup\$ PHP 7.4 fn($s)=>preg_replace('//', '!', $s); \$\endgroup\$ – Petah Aug 20 at 0:06
1
\$\begingroup\$

Charcoal, 6 bytes

⭆S⁺!ι!

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:

⪫!!⪫S!

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

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

SNOBOL4 (CSNOBOL4), 94 bytes

I	I =INPUT	:F(END)
S	I LEN(1) . X REM . I	:F(O)
	O ='!' O X	:(S)
O	OUTPUT =O '!'
	O =	:(I)
END

Try it online!

Prints with an additional trailing newline (as SNOBOL always prints a line break). The way input is consumed, there is no way of distinguishing between a final line ending in \n or not.

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

Stax, 6 5 bytes

_z'!R

Run and debug it

Replace "" with "!" using regex replacement. I don't exactly understand why this works.

Edit: Found it in the Ecma spec: 15.5.4.10

If there is a match with an emptystring (in other words, if the value of regexp.lastIndex is left unchanged), increment regexp.lastIndex by 1.

Old Stuff:

I would have been able to get to 5 bytes in either of two scenarios.

  1. If input was provided in an escaped literal e.g. "line1\nline2". But I opted against that for the sake of "usability".
  2. If there was not a bug feature in stax's zip implementation when encountering empty strings/arrays.
\$\endgroup\$
1
\$\begingroup\$

Scala (REPL), 15

s.mkString("!")
\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome! Input is not generally allowed to be a pre-assigned variable. It must be taken as an argument or from Stdin. Also, please consider adding an explanation or link to an interpreter or something, because code-only answers are automatically flagged as low-quality. \$\endgroup\$ – mbomb007 Aug 19 at 22:28
  • 1
    \$\begingroup\$ I think _.mkString("!") would be a valid submission \$\endgroup\$ – Jo King Aug 19 at 22:50
1
\$\begingroup\$

C#, 142 bytes

public class P{public static void Main(string[]a){System.Console.Write("!");foreach(char c in a[0]){System.Console.Write(c.ToString()+"!");}}}

EDIT: Full program now

\$\endgroup\$
  • \$\begingroup\$ You don't need to explicitly convert the character to a string: c+"!" works fine. :-) \$\endgroup\$ – ErikF Aug 19 at 18:28
  • 1
    \$\begingroup\$ This is a snippet. Answers on this site are required to be full programs or functions. \$\endgroup\$ – pppery Aug 19 at 21:01
  • \$\begingroup\$ Now it isn't ;P \$\endgroup\$ – canttalkjustcode Aug 19 at 23:56
1
\$\begingroup\$

C (gcc), 48 45 bytes

f(char*s){putchar(33);*s&&f(s+write(1,s,1));}

Try it online!

-3 thanks to ceilingcat

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

Python 3, 26 bytes

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

Try it online!

\$\endgroup\$
  • \$\begingroup\$ This exact answer has already been posted by Waterbyte \$\endgroup\$ – Reinstate Monica Aug 20 at 10:06
1
\$\begingroup\$

PowerShell, 21 bytes

"$args"-replace"","!"

Try it online!

Boring regex replacement is boring. ;-)

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

Wolfram Language (Mathematica), 49 48 42 bytes

#~StringSplit~""~StringRiffle~{a="!",a,a}&

Try it online!

-6 bytes thanks to @attinat

\$\endgroup\$
  • \$\begingroup\$ The code in the Header, Code, and Footer sections is run. You can use infix notation and drop the { } in StringSplit for 42 bytes \$\endgroup\$ – attinat Aug 19 at 23:24
  • \$\begingroup\$ @attinat Thanks that helps a lot! \$\endgroup\$ – Kai Aug 20 at 19:50
1
\$\begingroup\$

Brachylog, 10 bytes

"!"w.∋w?w⊥

Try it online!

Takes input through the output variable, and unifies the input variable with "!", printing the output.

"!"w          Print "!", which is the input variable.
    .∋w       Print an element of the output variable.
       ?w     Print the input variable again.
         ⊥    Fail.
\$\endgroup\$
1
\$\begingroup\$

Elvish, 32 bytes

use re;re:replace "" "!" (slurp)

Copy and paste into the Live Environment to try it out!

\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome to the site! Would it be possible to edit in a link to an online interpreter, such as Try it online! so that other users can verify your program? \$\endgroup\$ – caird coinheringaahing Aug 22 at 12:04
  • \$\begingroup\$ @cairdcoinheringaahing I added a link to the provided live environment. \$\endgroup\$ – kon Aug 22 at 13:36

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.