Phrase reversals

From Rosetta Code
Revision as of 08:48, 13 November 2014 by rosettacode>Bearophile (+ D entry)
Phrase reversals is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Given a string of space separated words containing the following phrase:

"rosetta code phrase reversal"
  1. Reverse the string.
  2. Reverse each individual word in the string, maintaining original string order.
  3. Reverse the order of each word of the phrase, maintaining the order of characters in each word.

Show your output here.

D

Partially lazy. <lang d>void main() {

   import std.stdio, std.range;
   immutable phrase = "rosetta code phrase reversal";
   phrase.retro.writeln;                          // Reversed string.
   phrase.splitter.map!retro.joiner(" ").writeln; // Words reversed.
   phrase.split.retro.joiner(" ").writeln;        // Word order reversed.

}</lang>

Output:
lasrever esarhp edoc attesor
attesor edoc esarhp lasrever
reversal phrase code rosetta

Python

<lang python>>>> phrase = "rosetta code phrase reversal" >>> phrase[::-1] # Reversed. 'lasrever esarhp edoc attesor' >>> ' '.join(word[::-1] for word in phrase.split()) # Words reversed. 'attesor edoc esarhp lasrever' >>> ' '.join(word for word in phrase.split()[::-1]) # Word order reversed. 'reversal phrase code rosetta' >>> </lang>