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.


#False = 0
#True = 1
 
Global *inputPtr.Character
 
Macro nextChar()
  *inputPtr + SizeOf(Character)
EndMacro
 
Procedure isPunctuation(c.s)
  If FindString("!?()[]{},.;:-'" + #DQUOTE$, c)
    ProcedureReturn #True
  EndIf
  ProcedureReturn #False
EndProcedure
 
Procedure oddWord()
  Protected c.c
  c = *inputPtr\c
  If isPunctuation(Chr(*inputPtr\c))
    ProcedureReturn
  Else
    nextChar()
    oddWord()
  EndIf 
  Print(Chr(c))
EndProcedure
 
Procedure oddWordProblem(inputStream.s)
  *inputPtr = @inputStream
  Define isOdd = #False
  While *inputPtr\c
    If isOdd
      oddWord()
    Else
      Repeat
        Print(Chr(*inputPtr\c))
        nextChar()
      Until isPunctuation(Chr(*inputPtr\c))
    EndIf
    Print(Chr(*inputPtr\c))
    isOdd ! 1 ;toggle word indicator
    nextChar()
  Wend 
EndProcedure
 
Define inputStream.s
If OpenConsole()
  Repeat
    PrintN(#CRLF$ + #CRLF$ + "Enter a series of words consisting only of English letters (i.e. a-z, A-Z)")
    PrintN("and that are separated by a punctuation mark (i.e. !?()[]{},.;:-' or " + #DQUOTE$ + ").")
    inputStream = Input()
    oddWordProblem(inputStream) ;assume input is correct
  Until inputStream = ""
 
  Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
  CloseConsole()
EndIf