Word wrap: Difference between revisions

Content added Content deleted
Line 8:
 
If you have both basic and extra credit solutions, show an example where the two algorithms give different results.
 
=={{header|AWK}}==
Basic word wrap.
 
<lang awk>function wordwrap_paragraph(p)
{
if ( length(p) < 1 ) return
split(p, words)
spaceLeft = lineWidth
line = words[1]
delete words[1]
 
for (i = 1; i <= length(words); i++) {
word = words[i]
if ( (length(word) + 1) > spaceLeft ) {
print line
line = word
spaceLeft = lineWidth - length(word)
} else {
spaceLeft -= length(word) + 1
line = line " " word
}
}
print line
}
 
BEGIN {
lineWidth = width
par = ""
}
 
/^[ \t]*$/ {
wordwrap_paragraph(par)
par = ""
}
 
!/^[ \t]*$/ {
par = par " " $0
}
 
END {
wordwrap_paragraph(par)
}</lang>
 
To test it,
 
<pre>
awk -f wordwrap.awk -v width=80 &lt; text.txt
</pre>
 
=={{header|Go}}==