How to resolve the algorithm Word wrap step by step in the Run BASIC programming language
How to resolve the algorithm Word wrap step by step in the Run BASIC 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 Run BASIC programming language
Source code in the run programming language
doc$ = "In olden times when wishing still helped one, there lived a king ";_
"whose daughters were all beautiful, but the youngest was so beautiful ";_
"that the sun itself, which has seen so much, was astonished whenever ";_
"it shone in her face."
wrap$ = " style='white-space: pre-wrap;white-space: -moz-pre-wrap;white-space: -pre-wrap;";_
"white-space: -o-pre-wrap;word-wrap: break-word'"
html ""
html "" + doc$ + " " + doc$ + "
"
doc$ = "In olden times when wishing still helped one, there lived a king
whose daughters were all beautiful, but the youngest was so beautiful
that the sun itself, which has seen so much, was astonished whenever
it shone in her face. Close by the king's castle lay a great dark
forest, and under an old lime-tree in the forest was a well, and when
the day was very warm, the king's child went out into the forest and
sat down by the side of the cool fountain, and when she was bored she
took a golden ball, and threw it up on high and caught it, and this
ball was her favorite plaything."
input "Width"; width ' user specifies width
while word$(doc$,i + 1," ") <> ""
i = i + 1
thisWord$ = word$(doc$,i," ") + " "
if word$(thisWord$,2,chr$(13)) <> "" then thisWord$ = word$(thisWord$,2,chr$(13)) + " " ' strip the
if len(docOut$) + len(thisWord$) > width then
print docOut$
docOut$ = ""
end if
docOut$ = docOut$ + thisWord$
wend
print docOut$
You may also check:How to resolve the algorithm Loops/Foreach step by step in the Tailspin programming language
You may also check:How to resolve the algorithm Strip comments from a string step by step in the Haskell programming language
You may also check:How to resolve the algorithm Arithmetic-geometric mean step by step in the ERRE programming language
You may also check:How to resolve the algorithm Doubly-linked list/Traversal step by step in the Nim programming language
You may also check:How to resolve the algorithm Currency step by step in the zkl programming language