Task

Write a program that solves the odd word problem with the restrictions given below.



Description

You are promised an input stream consisting of English letters and punctuations.

It is guaranteed that:

 the words (sequence of consecutive letters) are delimited by one and only one punctuation,
 the stream will begin with a word,
 the words will be at least one letter long,   and 
 a full stop (a period, [.]) appears after, and only after, the last word.


Example

A stream with six words:

 what,is,the;meaning,of:life. 


The task is to reverse the letters in every other word while leaving punctuations intact, producing:

 what,si,the;gninaem,of:efil. 
while observing the following restrictions:

 Only I/O allowed is reading or writing one character at a time, which means: no reading in a string, no peeking ahead, no pushing characters back into the stream, and no storing characters in a global variable for later use;
 You are not to explicitly save characters in a collection data structure, such as arrays, strings, hash tables, etc, for later reversal;
 You are allowed to use recursions, closures, continuations, threads, co-routines, etc., even if their use implies the storage of multiple characters.


Test cases

Work on both the   "life"   example given above, and also the text:

 we,are;not,in,kansas;any,more.


# 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
)