How to resolve the algorithm Word wrap step by step in the VBScript programming language
How to resolve the algorithm Word wrap step by step in the VBScript 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 VBScript programming language
Source code in the vbscript programming language
column = 60
text = "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."
Call wordwrap(text,column)
Sub wordwrap(s,n)
word = Split(s," ")
row = ""
For i = 0 To UBound(word)
If Len(row) = 0 Then
row = row & word(i)
ElseIf Len(row & " " & word(i)) <= n Then
row = row & " " & word(i)
Else
WScript.StdOut.WriteLine row
row = word(i)
End If
Next
If Len(row) > 0 Then
WScript.StdOut.WriteLine row
End If
End Sub
You may also check:How to resolve the algorithm Non-decimal radices/Output step by step in the Raku programming language
You may also check:How to resolve the algorithm Evaluate binomial coefficients step by step in the Julia programming language
You may also check:How to resolve the algorithm Random numbers step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Variable size/Get step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm One-dimensional cellular automata step by step in the Eiffel programming language