13
\$\begingroup\$

Challenge

Your goal is to write a function that puts quotes around a sequence of capital letters in a string.

So some examples (everything on the left (before function) and right (after function) are STRINGS):

Hello the name's John Cena -> "H"ello the name's "J"ohn "C"ena

I got BIG OLD truck -> "I" got "BIG" "OLD" truck

[OLD, joe, SMOE] ->["OLD", joe, "SMOE"]

'BIG') -> '"BIG"')

fOOd -> f"OO"d

"78176&@*#dd09)*@(&#*@9a0YOYOYOYOYOYOOYOYOYOY28#@e -> "78176&@*#dd09)*@(&#*@9a0"YOYOYOYOYOYOOYOYOYOY"28#@e

Assumptions and Clarifications

  • The sequence of capital letters can have a length of one (e.g. a string containing one character: H -> "H")
  • You can assume there will be only a string inputted.

Winning Criteria

Code golf, fewest bytes wins.

\$\endgroup\$
  • 4
    \$\begingroup\$ Great first question, well specified! \$\endgroup\$ – xnor Mar 16 at 17:40
  • 5
    \$\begingroup\$ Quite similar to Put in the quotes. \$\endgroup\$ – manatwork Mar 16 at 18:16
  • 1
    \$\begingroup\$ What is the domain - is it ASCII only? \$\endgroup\$ – Jonathan Allan Mar 16 at 18:35
  • 1
    \$\begingroup\$ I'm having difficulties seeing that first test case... :P. Probably because the time is now! \$\endgroup\$ – Lyxal Mar 17 at 4:14
  • 1
    \$\begingroup\$ I'm unable to see the words "John Cena"... Must be something to do with his phrase "you can't see me, my time is now". :P \$\endgroup\$ – Lyxal Mar 17 at 20:57

20 Answers 20

4
\$\begingroup\$

05AB1E, 13 bytes

0.ø.γ.u}'"ý¦¨

Try it online!

0.ø              # surround input string with 0s
   .γ  }         # group characters by:
     .u          #  is uppercase?
        '"ý      # join groups with quotes
           ¦¨    # remove the first and last characters (0s)
|improve this answer|||||
\$\endgroup\$
4
\$\begingroup\$

Perl 5, 15 bytes

s/[A-Z]+/"$&"/g

Try it online!

|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

Gema, 8 characters

<K>="$0"

Sample run:

bash-5.0$ gema '<K>="$0"' <<< 'Hello the name’s John Cena'
"H"ello the name’s "J"ohn "C"ena

Try it online!

|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

JavaScript (ES6),  32  30 bytes

Using a reference to the last match

Version suggested by @Grimmy
Saved 2 bytes thanks to @Shaggy

s=>s.replace(/[A-Z]+/g,'"$&"')

Try it online!

JavaScript (ES6), 35 bytes

With a callback function

s=>s.replace(/[A-Z]+/g,s=>`"${s}"`)

Try it online!

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ amazing nice job \$\endgroup\$ – Quora Expert Mar 16 at 17:41
  • 1
    \$\begingroup\$ 32 with backrefs \$\endgroup\$ – Grimmy Mar 16 at 17:43
  • \$\begingroup\$ Why not just $&? \$\endgroup\$ – Shaggy Mar 16 at 18:22
2
\$\begingroup\$

Python 3, 47 bytes

lambda s:re.sub('([A-Z]+)',r'"\1"',s)
import re

Try it online!

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

Japt, 11 bytes

r"%A+"`"$&"

Try it

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

Pyth, 20 bytes

Blatant port of RGS's Python answer. (I relized that I posted to the wrong challenge, whoops!)

:Q"([A-Z]+)""\"\\1\"

Try it online!

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ A shorter way to write the second string is s[N\\1N \$\endgroup\$ – isaacg Mar 17 at 19:51
2
\$\begingroup\$

sed, 26 21 bytes

Saved 5 bytes thanks to Xcali!!!

s/[[:upper:]]\+/"&"/g

Try it online!

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Using & cuts down on the byte count: Try it online! \$\endgroup\$ – Xcali Mar 17 at 2:28
  • \$\begingroup\$ @Xcali Nice one - thanks! :-) \$\endgroup\$ – Noodle9 Mar 17 at 10:10
1
\$\begingroup\$

Retina, 11 bytes

[A-Z]+
"$&"

Try it online!

I thought Retina has a shortcut for [A-Z] but apparently that only works in transliterations and not substitutions? Hmm...

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

V (vim), 9 bytes

ͨõ«©/"±"

Try it online!

This instructs Vim to replace all occurrences on all lines (Í)
...using the substitution \(\u\)\+/"\1". Each \x may be replaced by x with the high-bit set, thus \1 becomes ±, etc...

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

QuadR, 10 bytes

Port of @Adám's answer.

[A-Z]+
"&"

Try it online!

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ yo didn't realize ur question so similar my bad bro, got notified \$\endgroup\$ – Quora Expert Mar 17 at 1:16
1
\$\begingroup\$

C (gcc), 100 \$\cdots\$ 74 72 bytes

Saved 21 23 bytes thanks to ceilingcat!!!
Saved a byte thanks to S.S. Anne!!!

t;u;f(int*s){for(t=1;*s;printf(s++),t=u)(u=*s<65|*s>90)^t&&putchar(34);}

Try it online!

Uses wchar_t strings for input.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ @ceilingcat Wow! That's a great golf! Very readable in a golfy way: print quotes when we 're about to move in or out of caps. And of course that makes the putchar refactoring redundant - thanks! :-) \$\endgroup\$ – Noodle9 Mar 16 at 22:57
  • \$\begingroup\$ -1 with widechar \$\endgroup\$ – S.S. Anne Mar 16 at 23:36
  • \$\begingroup\$ Try adding an L before the string to make it a widechar string. \$\endgroup\$ – S.S. Anne Mar 16 at 23:46
  • \$\begingroup\$ @S.S.Anne Ah, that's a good one - thanks! :-) \$\endgroup\$ – Noodle9 Mar 17 at 0:06
  • \$\begingroup\$ @ceilingcat Huh, that's very interesting - thanks! :-) \$\endgroup\$ – Noodle9 Mar 17 at 1:20
1
\$\begingroup\$

Keg, 31 bytes

0&?(:A$Z$•[⑻[|1⑼\",],|⑻[\",]0⑾,

Try it online!

But seriously, don't ask who Joe is.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ wait, but, who's joe? \$\endgroup\$ – Quora Expert Mar 19 at 13:48
  • 1
    \$\begingroup\$ Joe mama! (I warned you not to ask! :P) \$\endgroup\$ – Lyxal Mar 19 at 22:00
  • 1
    \$\begingroup\$ LLLLLLLLLLOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL \$\endgroup\$ – Quora Expert Mar 19 at 22:25
  • \$\begingroup\$ Ha! Got'em! Haha XD lol! \$\endgroup\$ – Lyxal Mar 19 at 22:28
1
\$\begingroup\$

R, 34 bytes

gsub("([A-Z]+)","'\\1'",scan(,''))

Try it online!

Simple use of Regular Expressions.

39 bytes

cat(gsub('([A-Z]+)','"\\1"',scan(,'')))

Try it online!

This method is the only way I can get the double quotes to work.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ @JoKing Seems inconsequential to me. There was no specification on the type of quotes, but shockingly enough, the R code to switch it would be longer. \$\endgroup\$ – Sumner18 Mar 17 at 13:42
1
\$\begingroup\$

Ruby, 64 bytes

->s{s.chars.chunk{|c|c<c.downcase}.map{|u,c|u ??"+c*''+?":c}*''}

Try it online!

Longer than the other Ruby solution, but one that doesn't use regex. I also love an excuse to use chunk.

This groups successive characters of the string depending on whether or not they're uppercase, and then surround the ones that are uppercase with ". I had to do the downcase check rather than checking with upcase, because otherwise non-character strings (spaces, punctuation, etc.) would also be considered uppercase letters.

Golfy Tricks:

  • *'' instead of .join
  • < instead of !=
|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

AWK, 22 bytes

gsub("[A-Z]+","\"&\"")

Try it online!

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Charcoal, 20 bytes

F⁺S «ω✂"⁼№⪪α¹ω№αι≔ιω

Try it online! Link is to verbose version of code. Port of my answer to the question linked by @manatwork in the comments.

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Jelly, 11 bytes

e€ØAŻIœṗj”"

Try it online!

A monadic link taking a string and returning a string.

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

perl -pE, 16 bytes

s/\p{Lu}+/"$&"/g

Unicode compliant, unlike the s/[A-Z]+/"$&"/g solution presented earlier.

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Ruby, 30 bytes

->s{s.gsub(/([A-Z]+)/,'"\1"')}

Try it online!

A simple regex substitution.

|improve this answer|||||
\$\endgroup\$

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.