33
3
In chat, we are often fast-typers and don't really look at the order of letters before posting a message. Since we are lazy, we need a program that automatically swaps the last two letters in our words, but since we don't want to respond too late, the code must be short.
Your task, if you wish to accept it, is to write a program that flips the last two letters of each word in a given string (so the word Thansk
turns into Thanks
). A word is a sequence of two or more letters in the English alphabet delimited by a single space.
The string / list of characters you receive as input is guaranteed to only contain alphabetic characters and spaces (ASCII [97 - 122], [65 - 90] and 32).
You can take input and provide output through any standard method, in any programming language, while taking note that these loopholes are forbidden by default.
The output may have one trailing space and / or one trailing newline.
The input will always contain words only (and the corresponding whitespace) and will consist of at least one word.
This is code-golf, so the shortest submission (scored in bytes), in each language wins!
Test cases
Note that the strings are surrounded with quotes for readability.
Input -> Output "Thansk" -> "Thanks" "Youer welcoem" -> "Youre welcome" "This is an apple" -> "Thsi si na appel" "Flippign Lettesr Aroudn" -> "Flipping Letters Around" "tHe oDd chALlEneg wiht swappde lettesR" -> "teH odD chALlEnge with swapped letteRs"
Or, for test suite convenience, here are the inputs and their corresponding outputs separately:
Thansk Youer welcoem This is an apple Flippign Lettesr Aroudn tHe oDd chALlEneg wiht swappde lettesR
Thanks Youre welcome Thsi si na appel Flipping Letters Around teH odD chALlEnge with swapped letteRs
Thanks to DJMcMayhem for the title. This was originally a CMC.
May we output an array of words? – Shaggy – 2017-12-27T22:03:51.200
@Shaggy No, the output must be a string (or a list of characters by default) – Mr. Xcoder – 2017-12-27T22:04:47.303
May we request a trailing space on each input? – FlipTack – 2017-12-29T12:23:21.853
@FlipTack It was allowed in the initial version, but I have removed that rule before any of the answers that would use that had been posted. (partly because some users in chat told me I'm making this too easy otherwise, and I agree with them). No, it's not allowed.
– Mr. Xcoder – 2017-12-29T12:25:39.667Whta shoudl eb doen wiht oen lettre worsd liek "a"? – Fabian Röling – 2017-12-29T14:08:09.893
1@Fabian *A word is a sequence of two or more letters* – Mr. Xcoder – 2017-12-29T14:12:13.543