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===
<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).
@. =
<lang rexx>/*REXX program reverses the order of tokens in a string (but not the letters).*/
@.1 = "---------- Ice and Fire ------------"
@.=; @.1 = "---------- Ice and Fire ------------"
@.2 = ' '
@.2 = ' '
@.3 = "fire, in end will world the say Some"
@.4 = "ice. in say Some"
@.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 j=1 while @.j\==''; $= /*process each "line"; nullify $.*/
do j=1 while @.j\=='' /*process each of the 10 lines of poem.*/
do k=1 for words(@.j) /*process each word in the string*/
$= /*nullify the $ string (the new line)*/
$=word(@.j,k) $ /*prepend the word to a new line.*/
do k=1 for words(@.j) /*process each word in a @.j string.*/
end /*k*/ /* [↑] could do this another way*/
$=word(@.j,k) $ /*prepend a word to the new line ($). */
say $ /*display newly constructed line.*/
end /*k*/ /* [↑] we could do this another way. */

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:
end /*j*/ /*stick a fork in it, we're all done. */</lang>
'''output''' &nbsp; when using the (internal text) ten lines of input:
<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 = ' '
@.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''' &nbsp; is the same as the 1<sup>st</sup> REXX version. <br><br>


=={{header|Ruby}}==
=={{header|Ruby}}==