Odd word problem: Difference between revisions

Content added Content deleted
m (→‎{{header|Lua}}: minor restructure to eliminate EOF test (in case that could be considered "cheating", since period is guaranteed terminator))
m (→‎{{header|REXX}}: split compound statements, used a template for the output section, simplified the code.)
Line 2,266: Line 2,266:
The REXX program writes some header information to aid in visual fidelity when displaying the output to the
The REXX program writes some header information to aid in visual fidelity when displaying the output to the
<br>screen (also a blank line is written to make the screen display righteous; &nbsp; it's assumed that writing titles and
<br>screen (also a blank line is written to make the screen display righteous; &nbsp; it's assumed that writing titles and
<br>blank lines doesn't break the spirit of the restrictions (single character I/O) &nbsp; [the 8<sup>th</sup> line with the three <big><big>'''say'''</big></big>s].
<br>blank lines doesn't break the spirit of the restrictions (single character I/O) &nbsp; [the 8<sup>th</sup> line with the
three &nbsp; <big>'''say'''</big><small>s</small>].
<br>This displaying of informative messages is only to help the observer to know what is being performed.
<br>This displaying of informative messages is only to help the observer to know what is being performed.


No recursion or the stack is used. &nbsp; The program could've been written without subroutines.
No recursion or the stack is used. &nbsp; The program could've been written without subroutines.
<lang rexx>/*REXX program solves the odd word problem by only using byte input/output. */
<lang rexx>/*REXX program solves the odd word problem by only using (single) byte input/output.*/
iFID_ = 'ODDWORD.IN' /*Note: numeric suffix is added later.*/
iFID_ = 'ODDWORD.IN' /*Note: numeric suffix is added later.*/
oFID_ = 'ODDWORD.' /* " " " " " " */
oFID_ = 'ODDWORD.' /* " " " " " " */
do case=1 for 2; #= 0 /*#: is the number of characters read.*/

do case=1 for 2; #=0 /*#: is the number of characters read.*/
iFID= iFID_ || case /*read ODDWORD.IN1 or ODDWORD.IN2 */
iFID=iFID_ || case /*read ODDWORD.IN1 or ODDWORD.IN2 */
oFID= oFID_ || case /*write ODDWORD.1 or ODDWORD.2 */
oFID=oFID_ || case /*write ODDWORD.1 or ODDWORD.2 */
say; say; say '════════ reading file: ' iFID "════════" /* ◄■■■■■■■■■ optional. */
say; say; say '════════ reading file: ' iFID "════════" /* ◄■■■■■■■■■ optional. */


do until x==. /* [↓] perform for "odd" words.*/
do until x==. /* [↓] perform until reaching a period*/
do until \isMix(x); /* [↓] perform until punct found.*/
do until \datatype(x, 'M') /* [↓] " " punctuation found*/
call readChar; call writeChar /*read and write a letter. */
call readChar /*read a single character. */
end /*until ¬isMix(x)*/ /* [↑] keep reading " " */
call writeChar /*write " " " */
if x==. then leave /*is this the end─of─sentence ? */
end /*until \data···*/ /* [↑] read/write until punctuation. */
call readLetters; punct=# /*save the location of punctuation*/
if x==. then leave /*is this the end─of─sentence (period)?*/
do j=#-1 by -1; call readChar j /*read previous word (backwards). */
call readLetters; punct= # /*save the location of the punctuation.*/
if \isMix(x) then leave; call writeChar /*Found punctuation? Then leave. */
do j=#-1 by -1 /*read some characters backwards. */
end /*j*/ /* [↑] perform for "even" words.*/
call readChar j /*read previous word (backwards). */
if \datatype(x, 'M') then leave /*Found punctuation? Then leave J. */
call readLetters; call writeChar; #=punct /*read/write letters; new location*/
call writeChar /*write a character (which is a letter)*/
end /*j*/ /* [↑] perform for "even" words. */
call readLetters /*read a letter (and maybe punctuation)*/
call writeChar; #= punct /*write a char; punctuation location. */
end /*until x==.*/
end /*until x==.*/
end /*case*/ /* [↑] process both input files. */
end /*case*/ /* [↑] process both of the input files*/
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
isMix: return datatype( arg(1), 'M') /*return 1 if argument is a letter.*/
readLetters: do until \datatype(x, 'M'); call readChar; end; return
readLetters: do until \isMix(x); call readChar; end; return
writeChar: call charout , x /*console*/; call charout oFID, x /*file*/; return
writeChar: call charout , x; call charout oFID, x; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
readChar: if arg(1)=='' then do; x=charin(iFID); #=#+1; end /*read the next char*/
readChar: if arg(1)=='' then do; x= charin(iFID); #= #+1; end /*read next char*/
else x=charin(iFID, arg(1)) /* " specific " */
else x= charin(iFID, arg(1) ) /* " specific " */
return</lang>
return</lang>
'''output''' &nbsp; when using two (default) input files which contain:
{{out|output|text=&nbsp; when using the two (default) input files which contain:}}
:* &nbsp; input file &nbsp; '''ODDWORD.IN1''' &nbsp; ───► &nbsp; <tt> what,is,the;meaning,of:life. </tt>
:* &nbsp; input file &nbsp; '''ODDWORD.IN1''' &nbsp; ───► &nbsp; <tt> what,is,the;meaning,of:life. </tt>
:* &nbsp; input file &nbsp; '''ODDWORD.IN2''' &nbsp; ───► &nbsp; <tt> we,are;not,in,kansas;any,more. </tt>
:* &nbsp; input file &nbsp; '''ODDWORD.IN2''' &nbsp; ───► &nbsp; <tt> we,are;not,in,kansas;any,more. </tt>