Problem 007

Problem

Description

Resistance is Futile

Despite the instructor's abundant praise of graduate school [1], and his admonishment of the evils and terror of being a microserf, you have decided to follow Mr. Anderson [2] down the rabbit hole to the dark side; you are interviewing with the Redmond Beast.

Of course, the vile dark lords of the evil empire are quite selective of their enslaved brood and have devised fiendish interrogation puzzles; cruel twisted feats of mental prowess. These programming tortures are meant to weed out the weakest of the candidates so that the Borg has the pleasure of assimilating the souls of only the strongest and 1337.

The first of these devious interrogation problems is to reverse the words in a string in-place!!! Using pointers!!! [3]

Input

Consider the following Haiku:

Microsoft's evilz.
Open Source is FTW.
Fear not, the reaper.

Output

The results of successful reversal:

evilz Microsoft's.
FTW is Source Open.
reaper the, not Fear.

Trailing punctuation shall remain in place, but commas should be moved to the preceding word.

Note

This really is a common interview question. Even I had to do it for the Beast.

[1]Getting paid to go to school for another 6, 7, 8 years, what's not to love?
[2]My friend Dave. Not Neo.
[3]The horror, the horror.

Solution

Approach

Reverse string. Scan string and reverse individual words. While doing so, adjust commas if present.

Input/Output

Source Code