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).
@. =
<lang rexx>/*REXX pgmprogram reverses the order of tokens in a string, (but not the letters).*/
@.=; @.1 = "---------- Ice and Fire ------------"
@.2 = ' '
@.2 = ' '
@.3 = "fire, in end will world the say Some"
@.43 = "ice.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 = ' '
@.7 = ' '
@.8 = "... elided paragraph last ..."
@.9 = ' '
@.9 = ' '
@.10 = "Frost Robert -----------------------"
 
do j=1 while @.j\==''; $= /*process each "line";of the 10 lines nullifyof $poem.*/
$= do k=1 for words(@.j) /*processnullify eachthe word in$ the string (the new line)*/
$ do k=word1 for words(@.j,k) $ /*prependprocess theeach word toin a new line @.j string.*/
end /*$=word(@.j,k*/) $ /*prepend a word to /* [↑] the couldnew doline this another($). way*/
say $ end /*k*/ /* [↑] we /*displaycould newlydo constructedthis lineanother 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>
------------ 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''' &nbsp; is the same as the 1<sup>st</sup> REXX version. <br><br>
 
=={{header|Ruby}}==