Odd word problem: Difference between revisions

Added an Algol 68 sample
(Added zkl)
(Added an Algol 68 sample)
Line 83:
we,are;not,in,kansas;any,more.
we,era;not,ni,kansas;yna,more.</pre>
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
The words and punctuation should be on a single line. Uses recursion.
<lang algol68># recursively reverses the current word in the input and returns the #
# the character that followed it #
# "ch" should contain the first letter of the word on entry and will be #
# updated to the punctuation following the word on exit #
PROC reverse word = ( REF CHAR ch )VOID:
BEGIN
 
CHAR next ch;
 
read( ( next ch ) );
 
IF ( next ch <= "Z" AND next ch >= "A" )
OR ( next ch <= "z" AND next ch >= "a" )
THEN
reverse word( next ch )
FI;
 
print( ( ch ) );
 
ch := next ch
 
END; # reverse word #
 
 
 
# recursively prints the current word in the input and returns the #
# character that followed it #
# "ch" should contain the first letter of the word on entry and will be #
# updated to the punctuation following the word on exit #
PROC normal word = ( REF CHAR ch )VOID:
BEGIN
 
print( ( ch ) );
read ( ( ch ) );
 
IF ( ch <= "Z" AND ch >= "A" )
OR ( ch <= "z" AND ch >= "a" )
THEN
normal word( ch )
FI
 
END; # normal word #
 
 
 
# read and print words and punctuation from the input stream, reversing #
# every second word #
PROC reverse every other word = VOID:
BEGIN
 
CHAR ch;
 
read( ( ch ) );
 
WHILE
ch /= "."
DO
normal word( ch );
IF ch /= "."
THEN
print( ( ch ) );
read ( ( ch ) );
reverse word( ch )
FI
OD;
 
print( ( ch ) )
 
END; # reverse every other word #
 
 
 
main: (
reverse every other word
)</lang>
{{out}}
<pre>
what,si,the;gninaem,of:efil.
we,era;not,ni,kansas;yna,more.
</pre>
 
=={{header|C}}==
3,049

edits