Word wrap

From Rosetta Code
Revision as of 20:33, 27 March 2012 by Sonia (talk | contribs) (New task)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Word wrap is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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.

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 you have both basic and extra credit solutions, show an example where the two algorithms give different results.

Go

Basic task, no extra credit. <lang go>package main

import (

   "fmt"
   "strings"

)

func wrap(text string, lineWidth int) (wrapped string) {

   words := strings.Fields(text)
   if len(words) == 0 {
       return
   }
   wrapped = words[0]
   spaceLeft := lineWidth - len(wrapped)
   for _, word := range words[1:] {
       if len(word)+1 > spaceLeft {
           wrapped += "\n" + word
           spaceLeft = lineWidth - len(word)
       } else {
           wrapped += " " + word
           spaceLeft -= 1 + len(word)
       }
   }
   return

}

var frog = ` 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.`

func main() {

   fmt.Println("wrapped at 80:")
   fmt.Println(wrap(frog, 80))
   fmt.Println("wrapped at 72:")
   fmt.Println(wrap(frog, 72))

}</lang>

Output:
wrapped at 80:
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.
wrapped at 72:
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.