How to resolve the algorithm Word wrap step by step in the REXX programming language
How to resolve the algorithm Word wrap step by step in the REXX programming language
Table of Contents
Problem Statement
Even today, with proportional fonts and complex layouts, there are still cases where you need to wrap text at a specified column.
The basic task is to wrap a paragraph of text in a simple way in your language.
If there is a way to do this that is built-in, trivial, or provided in a standard library, show that. Otherwise implement the minimum length greedy algorithm from Wikipedia.
Show your routine working on a sample of text at two different wrap columns.
Wrap text using a more sophisticated algorithm such as the Knuth and Plass TeX algorithm.
If your language provides this, you get easy extra credit,
but you must reference documentation indicating that the algorithm
is something better than a simple minimum length algorithm.
If you have both basic and extra credit solutions, show an example where
the two algorithms give different results.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Word wrap step by step in the REXX programming language
Source code in the rexx programming language
/*REXX program reads a file and displays it to the screen (with word wrap). */
parse arg iFID width . /*obtain optional arguments from the CL*/
if iFID=='' | iFID=="," then iFID='LAWS.TXT' /*Not specified? Then use the default.*/
if width=='' | width=="," then width=linesize() /* " " " " " " */
@= /*number of words in the file (so far).*/
do while lines(iFID)\==0 /*read from the file until End-Of-File.*/
@=@ linein(iFID) /*get a record (line of text). */
end /*while*/
$=word(@,1) /*initialize $ with the first word. */
do k=2 for words(@)-1; x=word(@,k) /*parse until text (@) exhausted. */
_=$ x /*append it to the $ list and test. */
if length(_)>=width then do; say $ /*this word a bridge too far? > w. */
_=x /*assign this word to the next line. */
end
$=_ /*new words (on a line) are OK so far.*/
end /*m*/
if $\=='' then say $ /*handle any residual words (overflow).*/
/*stick a fork in it, we're all done. */
/*REXX program reads a file and displays it to the screen (with word wrap). */
parse arg iFID width kind _ . /*obtain optional arguments from the CL*/
if iFID=='' | iFID=="," then iFID = 'LAWS.TXT' /*Not specified? Then use the default.*/
if width=='' |width=="," then width= linesize() /* " " " " " " */
if right(width, 1) =='%' then width= linesize() * translate(width, , "%") % 100
if kind=='' | kind=="," then kind= 'Left' /*Default? Then use the default: LEFT */
just= left(kind, 1); upper just /*use 1st char of JUSTIFY, uppercased.*/
if pos(just, 'BCLR')==0 then call err "KIND (3rd arg) is illegal:" kind
if _\=='' then call err "too many arguments specified." _
if \datatype(width,'W') then call err "WIDTH (2nd arg) isn't an integer:" width
n=0 /*the number of words in the file. */
do j=0 while lines(iFID)\==0 /*read from the file until End-Of-File.*/
_=linein(iFID) /*get a record (line of text). */
do until _==''; n= n + 1 /*extract some words (or maybe not). */
parse var _ @.n _ /*obtain and assign next word in text. */
end /*until*/ /*parse 'til the line of text is null. */
end /*j*/
if j==0 then call err 'file' iFID "not found."
if n==0 then call err 'file' iFID "is empty (or has no words)"
$=@.1 /*initialize $ string with first word*/
do m=2 for n-1; x= @.m /*parse until text (@) is exhausted. */
_= $ x /*append it to the $ string and test.*/
if length(_)>= width then call tell /*this word a bridge too far? > w */
$= _ /*the new words are OK (so far). */
end /*m*/
call tell /*handle any residual words (if any). */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
err: say; say '***error***'; say; say arg(1); say; say; exit 13
/*──────────────────────────────────────────────────────────────────────────────────────*/
tell: if $=='' then return /* [↓] the first word may be too long.*/
w=max(width, length($) ) /*don't truncate long words (> w). */
select
when just=='L' then $= strip($) /*left ◄──────── */
when just=='R' then $= right($, w) /*──────► right */
when just=='B' then $=justify($, w) /*◄────both────► */
when just=='C' then $= center($, w) /* ◄centered► */
end /*select*/
say $ /*display the line of words to terminal*/
_= x /*handle any word overflow. */
return /*go back and keep truckin'. */
/* REXX ***************************************************************
* 20.08.2013 Walter Pachl "my way"
* 23.08.2013 Walter Pachl changed to use lastpos bif
**********************************************************************/
Parse Arg w
oid=w'.xxx'; 'erase' oid
Call o left(copies('123456789.',20),w)
s='She should have died hereafter;' ,
'There would have been a time for such a word.' ,
'Tomorrow, and tomorrow, and tomorrow, and so on'
Call ow s
Exit
ow:
Parse Arg s
s=s' '
Do While length(s)>w
i=lastpos(' ',s,w+1) /* instead of loop */
If i=0 Then
p=pos(' ',s)
Else
p=i
Call o left(s,p)
s=substr(s,p+1)
End
If s>'' Then
Call o s
Return
o:Return lineout(oid,arg(1))
You may also check:How to resolve the algorithm Caesar cipher step by step in the Haskell programming language
You may also check:How to resolve the algorithm Peano curve step by step in the Go programming language
You may also check:How to resolve the algorithm Inheritance/Single step by step in the J programming language
You may also check:How to resolve the algorithm Parsing/RPN to infix conversion step by step in the 11l programming language
You may also check:How to resolve the algorithm Angles (geometric), normalization and conversion step by step in the JavaScript programming language