Word wrap: Difference between revisions

Content added Content deleted
m (→‎{{header|Scala}}: Rm Scala imp category)
m ({{out}})
Line 1:
{{task}}
[[Category:Text processing]]
Even today, with proportional fonts and complex layouts, there are still [[Template:Lines_too_long|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 [http://en.wikipedia.org/wiki/Word_wrap#Minimum_length minimum length greedy algorithm from Wikipedia.]
 
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 [http://en.wikipedia.org/wiki/Word_wrap#Minimum_length minimum length greedy algorithm from Wikipedia.]
 
Show your routine working on a sample of text at two different wrap columns.
 
'''Extra credit!''' 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 minimimum length 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 minimimum length algorithm.
 
If you have both basic and extra credit solutions, show an example where the two algorithms give different results.
the two algorithms give different results.
 
=={{header|Ada}}==
Line 135 ⟶ 143:
end Wrap;</lang>
 
Output,{{out}} set to 72 lines (with input picked by cut-and-paste from the task description):
<pre>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
Line 170 ⟶ 178:
return, Result
}</lang>
{{OutputOut}}
<pre>72
In olden times when wishing still helped one, there lived a king whose
Line 272 ⟶ 280:
& out$(str$("\n80 columns:\n" wrap$(!Text.80)))
);</lang>
{{out}}
Output:
<pre>72 columns:
In olden times when wishing still helped one, there lived a king whose
Line 611 ⟶ 619:
}
}</lang>
{{out}}
Output:
<pre>------------------------------------------------------------------------
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas varius
Line 672 ⟶ 680:
(println line))</lang>
 
{{out}}
Output :
<pre>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
Line 867 ⟶ 875:
printfn ""
0</lang>
{{out}}
Output
<pre style="font-size:smaller">------------------------------------------------------------------------
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas varius
Line 1,018 ⟶ 1,026:
println wordWrap(text)
println wordWrap(text, 120)</lang>
{{out}}
Output:
<pre>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
Line 1,212 ⟶ 1,220:
</lang>
 
{{out}}
Output:
<pre>
DEFAULT:
Line 1,436 ⟶ 1,444:
print(textwrap(example1, 60))</lang>
 
{{out}}
'''Output:'''
 
<pre>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
Line 1,483 ⟶ 1,490:
StringJoin@@Riffle[masterString,"\n"]
];</lang>
Output{{out}} for width 72 and 80:
<lang>wordWrap[72, 1, string]
wordWrap[80, 1, string]</lang>
{{out}}
Output:
<pre>In olden times when wishing still helped one, there lived a king
whose daughters were all beautiful, but the youngest was so beautiful
Line 1,645 ⟶ 1,652:
echo wordWrap(txt, 45)
</lang>
{{out}}
Output:
<pre>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus.
Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec
Line 1,733 ⟶ 1,740:
wrap(King, 50)</lang>
 
{{out}}
Output:
<pre>
And so let freedom ring from the prodigious hilltops of New Hampshire; let
Line 1,845 ⟶ 1,852:
Say time('E')
</pre>
{{out}}
Output:
<pre>
A nnnnnnnnnnnnnn ooooooooooooooo nnnnnnnnnnnnnn
Line 1,980 ⟶ 1,987:
if $\=='' then say $ /*handle any residual words. */
/*stick a fork in it, we're done.*/</lang>
'''output'''{{out}} is the same as version 1 using the &nbsp; '''L'''eft &nbsp; option (the default).
 
===version 1===
Line 2,144 ⟶ 2,151:
∙ Parameters are variables travelling under an assumed name.
</pre>
'''output'''{{out}} when specifying: &nbsp; <tt> , 155 </tt>
<pre style="height:40ex">
────────── Computer programming laws ────────── The Primal Scenario -or- Basic Datum of Experience: ∙ Systems in general work poorly or not at all. ∙
Line 2,182 ⟶ 2,189:
assumed name.
</pre>
Output{{out}} when specifying: <tt> , 77 </tt>
<pre style="height:40ex">
────────── Computer programming laws ────────── The Primal Scenario -or-
Line 2,256 ⟶ 2,263:
name.
</pre>
Output{{out}} [justified] when specifying: <tt> , 70 both </tt>
<pre style="height:40ex">
────────── Computer programming laws ────────── The Primal Scenario -or-
Line 2,360 ⟶ 2,367:
Return
o:Return lineout(oid,arg(1))</lang>
Output{{out}} for widths 72 and 9
<pre>
123456789.123456789.123456789.123456789.123456789.123456789.123456789.12
Line 2,713 ⟶ 2,720:
FILE "text" = wrappedtext
</lang>
{{out}}
Output:
<pre style='height:30ex;overflow:scroll'>
--------------------------------------------------------------------------------