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; it's assumed that writing titles and |
<br>screen (also a blank line is written to make the screen display righteous; it's assumed that writing titles and |
||
<br>blank lines doesn't break the spirit of the restrictions (single character I/O) [the 8<sup>th</sup> line with the |
<br>blank lines doesn't break the spirit of the restrictions (single character I/O) [the 8<sup>th</sup> line with the |
||
three <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. The program could've been written without subroutines. |
No recursion or the stack is used. The program could've been written without subroutines. |
||
<lang rexx>/*REXX program |
<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.*/ |
|||
oFID_ = 'ODDWORD.' /* " " " " " " */ |
|||
do case=1 for 2; #= 0 /*#: is the number of characters read.*/ |
|||
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==. |
do until x==. /* [↓] perform until reaching a period*/ |
||
do until \ |
do until \datatype(x, 'M') /* [↓] " " punctuation found*/ |
||
call readChar |
call readChar /*read a single character. */ |
||
call writeChar /*write " " " */ |
|||
end /*until \data···*/ /* [↑] read/write until punctuation. */ |
|||
if x==. then leave /*is this the end─of─sentence (period)?*/ |
|||
call readLetters; punct= # /*save the location of the punctuation.*/ |
|||
do j=#-1 by -1 /*read some characters backwards. */ |
|||
call readChar j /*read previous word (backwards). */ |
|||
if \datatype(x, 'M') then leave /*Found punctuation? Then leave J. */ |
|||
⚫ | |||
call writeChar /*write a character (which is a letter)*/ |
|||
end /*j*/ /* [↑] perform for "even" words. */ |
|||
call readLetters /*read a letter (and maybe punctuation)*/ |
|||
⚫ | |||
end /*until x==.*/ |
end /*until x==.*/ |
||
end /*case*/ |
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. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
readLetters: do until \datatype(x, 'M'); 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 |
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> |
||
{{out|output|text= when using the two (default) input files which contain:}} |
|||
:* input file '''ODDWORD.IN1''' ───► <tt> what,is,the;meaning,of:life. </tt> |
:* input file '''ODDWORD.IN1''' ───► <tt> what,is,the;meaning,of:life. </tt> |
||
:* input file '''ODDWORD.IN2''' ───► <tt> we,are;not,in,kansas;any,more. </tt> |
:* input file '''ODDWORD.IN2''' ───► <tt> we,are;not,in,kansas;any,more. </tt> |