Scrambled Words
October 8, 2019
Given a string, scramble the letters of each word individually, where a word is a maximal sequence of letters, maintaining the original capitalization and punctuation. For instance, the string “Programming Praxis is fun!” might be scrambled as “Grprnimaogm Srxpia is unf!”
Your task is to write a program to scramble the letters of each word in a string. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Nice little drill. Here is my take on it using Julia 1.1.1: https://pastebin.com/WsrMBkKD. Cheers!
Favourite language again – this time a bit golfish – so I apologies in advance
Expanding this out a bit – with comments…
Just tweaked it – that was a bit too long…
Here is a solution in R7RS Scheme, emphasizing clarity over length or
efficiency. It is quite similar to @programmingpraxis’s solution, but
has no external dependencies (other than a couple of procedures from
the popular SRFIs 1 and 27).
Output:
A straightforward C++ solution, using library functions for scanning strings and shuffling – I’d originally used a FSA, but using find_if etc. is quite neat:
I probably should have included the string header there and I’m not sure why the compiler didn’t complain about missing the std:: for find_if and find_if_not.
Here’s a solution in Python.
Output:
Here’s a Haskell version.
[…] the previous exercise, we wrote a program to scramble the letters in each word of a string, retaining capitalization and […]
[…] program to scramble the letters of each word in a string. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments […]