Word wrap: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
No edit summary
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 829:
return 0;
}</lang>
 
=={{header|C++}}==
Basic task.
{{trans|Go}}
<lang cpp>#include <iostream>
#include <sstream>
#include <string>
 
const char *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."
};
 
std::string wrap(const char *text, size_t line_length = 72)
{
std::istringstream words(text);
std::ostringstream wrapped;
std::string word;
 
if (words >> word) {
wrapped << word;
size_t space_left = line_length - word.length();
while (words >> word) {
if (space_left < word.length() + 1) {
wrapped << '\n' << word;
space_left = line_length - word.length();
} else {
wrapped << ' ' << word;
space_left -= word.length() + 1;
}
}
}
return wrapped.str();
}
 
int main()
{
std::cout << "Wrapped at 72:\n" << wrap(text) << "\n\n";
std::cout << "Wrapped at 80:\n" << wrap(text, 80) << "\n";
}</lang>
{{out}}
<pre>
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.
 
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.</pre>
 
=={{header|C sharp}}==
Line 992 ⟶ 922:
venenatis feugiat, augue orci pellentesque risus, nec pretium lacus enim eu
nibh.</pre>
 
=={{header|C++}}==
Basic task.
{{trans|Go}}
<lang cpp>#include <iostream>
#include <sstream>
#include <string>
 
const char *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."
};
 
std::string wrap(const char *text, size_t line_length = 72)
{
std::istringstream words(text);
std::ostringstream wrapped;
std::string word;
 
if (words >> word) {
wrapped << word;
size_t space_left = line_length - word.length();
while (words >> word) {
if (space_left < word.length() + 1) {
wrapped << '\n' << word;
space_left = line_length - word.length();
} else {
wrapped << ' ' << word;
space_left -= word.length() + 1;
}
}
}
return wrapped.str();
}
 
int main()
{
std::cout << "Wrapped at 72:\n" << wrap(text) << "\n\n";
std::cout << "Wrapped at 80:\n" << wrap(text, 80) << "\n";
}</lang>
{{out}}
<pre>
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.
 
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.</pre>
 
=={{header|Clojure}}==
Line 2,065:
 
</lang>
 
 
=={{header|JavaScript}}==
Line 2,622 ⟶ 2,621:
Wikipedia.
</pre>
 
=={{header|M2000 Interpreter}}==
M2000 Environment (where actual a M2000 script executed) provide the means to print proportional text, in console, in layer in console form, in user windows layers, and in printer paper. To render proportional text we can use:
Line 2,803:
eleven!
</pre>
 
 
=={{header|NetRexx}}==
Line 3,130 ⟶ 3,129:
$_ = $s;
s/\s*(.{1,25})\s/$1\n/g, print;</lang>
=={{header|Perl 6}}==
<lang perl6>my $s = "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.";
 
$s ~~ s:g/»\s+/ /;
$s ~~ s/\s*$/\n\n/;
 
say $s.subst(/ \s* (. ** 1..66) \s /, -> $/ { "$0\n" }, :g);
say $s.subst(/ \s* (. ** 1..25) \s /, -> $/ { "$0\n" }, :g);</lang>
 
=={{header|Phix}}==
Line 3,721 ⟶ 3,704:
(wrap (string-split text) 70)
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>my $s = "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.";
 
$s ~~ s:g/»\s+/ /;
$s ~~ s/\s*$/\n\n/;
 
say $s.subst(/ \s* (. ** 1..66) \s /, -> $/ { "$0\n" }, :g);
say $s.subst(/ \s* (. ** 1..25) \s /, -> $/ { "$0\n" }, :g);</lang>
 
=={{header|REXX}}==
Line 4,696 ⟶ 4,697:
threw it up on high and caught it, and this ball was her favorite
plaything.</pre>
 
=={{header|TPP}}==
The text presentation program automatically provides word wrap:
10,327

edits