Skip to main content
-8 bytes swapping the order of the segments from the string and changing the approach in the `s///`ubstitution.
Source Link
Dom Hastings
  • 24.4k
  • 4
  • 56
  • 93

Perl 5 + -pap, 4436 bytes

%a=map%a=<>=~/(.\S)(.*\S*)/g,@F;$_=<>;sg;s/(.)\K/$a$&$a{lc$1lc$&}/g

Try it online!Try it online!

Explanation

Using @F (populated and -auto-split on whitespace from the first line of input that contains the segments), theThe hash %a is built using the first letter of each segment (from <>, STDIN) as the key and any remaining letters (0 or more) as the value. Then $_ is populated with the next line of input and a s///ubstitution is performed on $_ replacing each character in the string with itself (but \K$&eeping it in place) withfollowed by the value from the hash at (the lowercase) key in %a. $_ is then implicitly -printed.

Perl 5 + -pa, 44 bytes

%a=map/(.)(.*)/g,@F;$_=<>;s/(.)\K/$a{lc$1}/g

Try it online!

Explanation

Using @F (populated and -auto-split on whitespace from the first line of input that contains the segments), the hash %a is built using the first letter of each segment as the key and any remaining letters (0 or more) as the value. Then $_ is populated with the next line of input and a s///ubstitution is performed on $_ replacing each character in the string (but \Keeping it in place) with the value from the hash at (the lowercase) key in %a. $_ is then implicitly -printed.

Perl 5 + -p, 36 bytes

%a=<>=~/(\S)(\S*)/g;s/./$&$a{lc$&}/g

Try it online!

Explanation

The hash %a is built using the first letter of each segment (from <>, STDIN) as the key and any remaining letters (0 or more) as the value. Then s///ubstitution is performed on $_ replacing each character in the string with itself ($&) followed by the value from the hash at (the lowercase) key in %a. $_ is then implicitly -printed.

Source Link
Dom Hastings
  • 24.4k
  • 4
  • 56
  • 93

Perl 5 + -pa, 44 bytes

%a=map/(.)(.*)/g,@F;$_=<>;s/(.)\K/$a{lc$1}/g

Try it online!

Explanation

Using @F (populated and -auto-split on whitespace from the first line of input that contains the segments), the hash %a is built using the first letter of each segment as the key and any remaining letters (0 or more) as the value. Then $_ is populated with the next line of input and a s///ubstitution is performed on $_ replacing each character in the string (but \Keeping it in place) with the value from the hash at (the lowercase) key in %a. $_ is then implicitly -printed.