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: | Line 1,506: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===natural order=== |
|||
⚫ | |||
This REXX version process the words in a natural order (first to last). |
|||
@. = |
|||
⚫ | |||
@.1 = "---------- Ice and Fire ------------" |
@.=; @.1 = "---------- Ice and Fire ------------" |
||
@.2 = ' ' |
|||
@.2 = ' ' |
|||
⚫ | |||
@. |
@.3 = "fire, in end will world the say Some" |
||
@.4 = "ice. in say Some" |
|||
@.5 = "desire of tasted I've what From" |
@.5 = "desire of tasted I've what From" |
||
@.6 = "fire. favor who those with hold I" |
@.6 = "fire. favor who those with hold I" |
||
@.7 = ' ' |
|||
@.7 = ' ' |
|||
@.8 = "... elided paragraph last ..." |
@.8 = "... elided paragraph last ..." |
||
@.9 = ' ' |
|||
@.9 = ' ' |
|||
@.10 = "Frost Robert -----------------------" |
@.10 = "Frost Robert -----------------------" |
||
do |
do j=1 while @.j\=='' /*process each of the 10 lines of poem.*/ |
||
$= /*nullify the $ string (the new line)*/ |
|||
do k=1 for words(@.j) /*process each word in a @.j string.*/ |
|||
$=word(@.j,k) $ /*prepend a word to the new line ($). */ |
|||
end /*k*/ /* [↑] we could do this another way. */ |
|||
⚫ | |||
say $ /*display the newly constructed line. */ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
<pre> |
<pre> |
||
------------ Fire and Ice ---------- |
------------ Fire and Ice ---------- |
||
Line 1,538: | Line 1,541: | ||
----------------------- Robert Frost |
----------------------- Robert Frost |
||
</pre> |
</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 = ' ' |
|||
⚫ | |||
@.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}}== |
=={{header|Ruby}}== |