Jump to content

Odd word problem: Difference between revisions

m
→‎{{header|REXX}}: split compound statements, used a template for the output section, simplified the code.
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:
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>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].
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.
 
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 (single) byte input/output. */
iFID_ = 'ODDWORD.IN' 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 caseiFID=1 iFID_ for 2;|| case #=0 /*#:read is theODDWORD.IN1 number ofor characters readODDWORD.IN2 */
iFIDoFID=iFID_ oFID_ || case /*read write ODDWORD.IN11 or ODDWORD.IN22 */
oFID=oFID_ || case /*write ODDWORD.1 or ODDWORD.2 */
say; say; say '════════ reading file: ' iFID "════════" /* ◄■■■■■■■■■ optional. */
 
do until x==. /* [↓] perform for until "odd"reaching a words.period*/
do until \isMixdatatype(x, 'M'); /* [↓] " " /* [↓] perform until punctpunctuation found.*/
call readChar; call writeChar /*read and write a lettersingle character. */
end /*until ¬isMix(x)*/call writeChar /*write [↑]" " keep reading " " */
if x==. then leave end /*until \data···*/ /*is this[↑] the read/write end─of─sentenceuntil ?punctuation. */
callif readLetters;x==. then leave punct=# /*saveis thethis locationthe ofend─of─sentence punctuation(period)?*/
call readLetters; do j=#-1 by -1; call readChar j punct= # /*save the location /*readof previousthe word (backwards)punctuation. */
if \isMix(x) then leave;do j=#-1 by -1 call writeChar /*Foundread punctuation?some characters Then leavebackwards. */
end /*j*/ call readChar j /*read previous word (backwards). /* [↑] perform for "even" words.*/
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 readLetterswriteChar; call writeChar; #= punct /*read/write lettersa char; new punctuation location. */
end /*until x==.*/
end /*case*/ /* [↑] process both of the input files. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
isMixreadLetters: do until return \datatype( arg(1)x, 'M'); call readChar; /*return 1 if argument isend; a letter.*/ return
readLetterswriteChar: do untilcall charout , \isMix(x); /*console*/; call readChar;charout oFID, x end/*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*/
else x= charin(iFID, arg(1) ) /* " specific " */
return</lang>
'''{{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.IN2''' &nbsp; ───► &nbsp; <tt> we,are;not,in,kansas;any,more. </tt>
Cookies help us deliver our services. By using our services, you agree to our use of cookies.