Reverse words in a string: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: added/changed whitespace and comments, added a 2nd REXX version.) |
|||
Line 1,506:
=={{header|REXX}}==
===natural order===
<lang rexx>/*REXX pgm reverses the order of tokens in a string, but not the letters*/▼
This REXX version process the words in a natural order (first to last).
@.=; @.1 = "---------- Ice and Fire ------------"
@.2 = ' '
@.3 = "fire, in end will world the say Some"▼
@.
@.4 = "ice. in say Some"
@.5 = "desire of tasted I've what From"
@.6 = "fire. favor who those with hold I"
@.7 = ' '
@.8 = "... elided paragraph last ..."
@.9 = ' '
@.10 = "Frost Robert -----------------------"
do
$=
end /*j*/ /*stick a fork in it, we're done.*/</lang>▼
say $ /*display the newly constructed line. */
'''output''' when using the ten lines of input:▼
▲'''output''' when using the (internal text) ten lines of input:
<pre>
------------ Fire and Ice ----------
Line 1,538 ⟶ 1,541:
----------------------- Robert Frost
</pre>
===reverse order===
This REXX version process the words in reverse order (last to first).
<lang rexx>/*REXX program reverses the order of tokens in a string (but not the letters).*/
@.=; @.1 = "---------- Ice and Fire ------------"
@.2 = ' '
▲ @.3 = "fire, in end will world the say Some"
@.4 = "ice. in say Some"
@.5 = "desire of tasted I've what From"
@.6 = "fire. favor who those with hold I"
@.7 = ' '
@.8 = "... elided paragraph last ..."
@.9 = ' '
@.10 = "Frost Robert -----------------------"
do j=1 while @.j\=='' /*process each of the 10 lines of poem.*/
$= /*nullify the $ string (the new line)*/
do k=words(@.j) to 1 by -1 /*process each word in a @.j string.*/
$=$ word(@.j,k) /*append a word to the new line ($). */
end /*k*/ /* [↑] process last word to first word*/
say $ /*display the newly constructed line. */
end /*j*/ /*stick a fork in it, we're all done. */</lang>
'''output''' is the same as the 1<sup>st</sup> REXX version. <br><br>
=={{header|Ruby}}==
|