Jump to content

Reverse words in a string: Difference between revisions

Added Algol 68
(Added Algol 68)
Line 144:
----------------------- Robert Frost</pre>
 
 
=={{header|ALGOL 68}}==
<lang algol68># returns original phrase with the order of the words reversed #
# a word is a sequence of non-blank characters #
PROC reverse word order = ( STRING original phrase )STRING:
BEGIN
STRING words reversed := "";
STRING separator := "";
INT start pos := LWB original phrase;
WHILE
# skip leading spaces #
WHILE IF start pos <= UPB original phrase
THEN original phrase[ start pos ] = " "
ELSE FALSE
FI
DO start pos +:= 1
OD;
start pos <= UPB original phrase
DO
# have another word, find it #
INT end pos := start pos;
WHILE IF end pos <= UPB original phrase
THEN original phrase[ end pos ] /= " "
ELSE FALSE
FI
DO end pos +:= 1
OD;
( original phrase[ start pos : end pos - 1 ] + separator ) +=: words reversed;
separator := " ";
start pos := end pos + 1
OD;
words reversed
END # reverse word order # ;
# reverse the words in the lines as per the task #
print( ( reverse word order ( "--------- Ice and Fire ------------ " ), newline ) );
print( ( reverse word order ( " " ), newline ) );
print( ( reverse word order ( "fire, in end will world the say Some" ), newline ) );
print( ( reverse word order ( "ice. in say Some " ), newline ) );
print( ( reverse word order ( "desire of tasted I've what From " ), newline ) );
print( ( reverse word order ( "fire. favor who those with hold I " ), newline ) );
print( ( reverse word order ( " " ), newline ) );
print( ( reverse word order ( "... elided paragraph last ... " ), newline ) );
print( ( reverse word order ( " " ), newline ) );
print( ( reverse word order ( "Frost Robert -----------------------" ), newline ) )
</lang>
{{out}}
<pre>
------------ Fire and Ice ---------
 
Some say the world will end in fire,
Some say in ice.
From what I've tasted of desire
I hold with those who favor fire.
 
... last paragraph elided ...
 
----------------------- Robert Frost
</pre>
 
=={{header|AppleScript}}==
3,048

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.