Abbreviate everything
+4
−0
Challenge
Make a program that takes input of a string and abbreviate it.
- All letters of an abbreviation are capitalized, so keep that in mind.
- Whitespace, numbers and non-English characters are ignored.
- If the inputted string is a paragraph (2+ sentences), the abbreviation will be split by spaces (
) where.,?or!are, while commas (,) are ignored in this splitting scenario. - Words that are connected by hyphens (
-) will result to only one letter. - The only non-alphabetical characters to remain in the result are colons (
:) and semicolons (;). - This is code-golf, so shortest program in each language wins.
Test Cases
// Input
# Output
Hello, World!
HW
Can you make me a sandwich?
CYMMAS
hyper-neutrino
H
Too long; didn't read
TL;DR
I studied, don't worry. I will pass.
ISDW IWP
What???!?!
W
Get me 99 bottles of beer, bro!
GMBOBB
+3
−0
[Haskell], 139 138 bytes …
5y ago
+3
−0
[Python 3], 142 bytes d …
5y ago
+3
−0
[JavaScript (Node.js)], 91 68 …
5y ago
+1
−0
Perl, 62 bytes Reads a line …
5mo ago
+0
−0
Japt v2.0a0 `-S`, 26 bytes …
4mo ago
5 answers
+3
−0
Haskell, 139 138 bytes
import Data.Char
f(a:b)|d a=toUpper a:f(dropWhile(\a->e a"-'"||d a)b)|e a":;"=a:c|e a".?!"=' ':c|0<1=c where c=f b;d=isLetter;e=elem
f a=a
0 comment threads
+3
−0
JavaScript (Node.js), 91 68 65 bytes
s=>s.toUpperCase().match(/(?<=^| )[A-Z]|:|;|(?<=[.?!]) /g).join``
Assumes that the original text is properly formatted (has a space after punctuation). It converts the string to uppercase, then matches the following:
- A letter at the beginning of the string or preceded by a space
-
:or; - A space preceded by
.,?, or!
0 comment threads
+3
−0
Python 3, 142 bytes
def f(s):
m=1;r=""
for c in s.upper():
if'@'<c<'['or'-'==c:r+=c*m;m=0
if c in":; ":r+=c*(c!=' ');m=1
if c in".?!":r+=' ';m=1
return r
0 comment threads
+1
−0
Perl, 62 bytes
Reads a line from stdin. Prints abbreviation to stdout.
perl -pe'y/ .!?/! /s;s/[^:;a-z ]*([a-z])[^:;! ]*!?| $/\u$1/gi'
- convert
.!?to space and space to!- then squeeze duplicate runs
- perform case-insensitive substitutions
- don't touch
:,;or space - OR consume everything up to
!(or EOL)- then replace with the first letter as uppercase
- OR discard trailing space
- don't touch

0 comment threads