For this challenge, you will have to write a function called capitalizeFirstLast or capitalize_first_last. This function will capitalize the first and last letter of each word, and lowercase what is in between.
capitalizeFirstLast "and still i rise" -- "AnD StilL I RisE"
Rules:
- The function will take a single parameter, which will be a string.
- The string can contain words separated by a single space.
- Words are made of letters from the ASCII range only.
- The function should return a string.
- Only the first and last letters are uppercased.
- All the other letters should be lowercased.
Examples:
capitalizeFirstLast "and still i rise" -- "AnD StilL I RisE"
capitalizeFirstLast "when words fail music speaks" -- "WheN WordS FaiL MusiC SpeakS"
capitalizeFirstLast "WHAT WE THINK WE BECOME" -- "WhaT WE ThinK WE BecomE"
capitalizeFirstLast "dIe wITh mEMORIEs nOt dREAMs" -- "DiE WitH MemorieS NoT DreamS"
capitalizeFirstLast "hello" -- "HellO"
Good luck!
This challenge comes from aminnairi here on DEV. Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Latest comments (24)
codepen.io/FlorinCornea/pen/BaNQVV...
Not the best-looking of all, but it works. C#, with a lot of fiddling with Join and Select.
And, to test it.
Came up with this Elm solution while I was procrastinating at work today:
Not overly different from the other solutions here, but neat nonetheless.
Python
JSBin Link
python
Haskell
An easy option is to start off by lowercasing the whole thing...
(JS)
Regex matches single letters preceded by a word boundary or followed by a word boundary.
Running through the examples:
Nice! Super clean.
Thanks!
Javascript + Regex "One" liner.
Click here if you're curious what the regular expression does
JAVASCRIPT ANSWER