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.


define odd_word_processor(str::string) => {
	local(
		isodd 		= false,
		pos 		= 1,
		invpos	 	= 1,
		lastpos		= 1
	)
	while(#str->get(#pos) != '.' && #pos <= #str->size) => {
		if(not #str->isAlpha(#pos)) => {
			not #isodd ? #isodd = true | #isodd = false	
		}
		if(#isodd) => {
			#lastpos = 1
			#invpos = 1
			while(#str->isAlpha(#pos+#lastpos) && #pos+#lastpos <= #str->size) => {
				#lastpos++
			}
			'odd lastpos: '+#lastpos+'\r'
			local(maxpos = #pos+#lastpos-1)
			while(#invpos < (#lastpos+1)/2) => {
				local(i,o,ipos,opos)
				#ipos = #pos+#invpos
				#opos = #pos+(#lastpos-#invpos)
				#i = #str->get(#ipos)
				#o = #str->get(#opos)
 
				//'switching '+#i+' and '+#o+'\r'
 
				//'insert '+#o+' at '+(#ipos)+'\r'
				#str = string_insert(#str,-position=(#ipos),-text=#o)
 
				//'remove redundant pos '+(#ipos+1)+'\r'
				#str->remove(#ipos+1,1)
 
				//'insert '+#i+' at '+(#opos)+'\r'
				#str = string_insert(#str,-position=(#opos),-text=#i)
 
				//'remove redundant pos '+(#opos+1)+'\r'
				#str->remove(#opos+1,1)
 
				#invpos++
			}
			#pos += #lastpos - 1
		}
		//#str->get(#pos) + #isodd + '\r'
		#pos += 1
	}
	return #str
}
 
'orig:\rwhat,is,the;meaning,of:life.\r'
'new:\r'
odd_word_processor('what,is,the;meaning,of:life.')
'\rShould have:\rwhat,si,the;gninaem,of:efil.'