RosettaCodeData/Task/Word-wrap/Seed7/word-wrap.seed7
Ingy döt Net 776bba907c Sync
2013-10-27 22:24:23 +00:00

44 lines
1.6 KiB
Text

$ include "seed7_05.s7i";
const func string: wrap (in string: aText, in integer: lineWidth) is func
result
var string: wrapped is "";
local
var array string: words is 0 times "";
var string: word is "";
var integer: spaceLeft is 0;
begin
words := split(aText, " ");
if length(words) <> 0 then
wrapped := words[1];
words := words[2 ..];
spaceLeft := lineWidth - length(wrapped);
for word range words do
if length(word) + 1 > spaceLeft then
wrapped &:= "\n" & word;
spaceLeft := lineWidth - length(word);
else
wrapped &:= " " & word;
spaceLeft -:= 1 + length(word);
end if;
end for;
end if;
end func;
const proc: main is func
local
const string: frog is "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.";
var integer: width is 0;
begin
for width range [] (72, 80) do
writeln("Wrapped at " <& width <& ":");
writeln(wrap(frog, width));
end for;
end func;