Hi,
on Wed, 22 Mar 2000 at 22:13:57 o'clock, Ryan Lemenchick wrote:
Quote
> Problem: I want to read in a string (menu item) and when it gets to a
> certain number of characters stop, then print the remaining characters on
> the next line. B.T.W this will be done in the Drawing Window in Think
> Pascal....
Then you are in the wrong newsgroup, but what the hell...
A very simple algorithm would start at the beginning of the string with
two counters, one for iterating the string, and one for counting the
screen columns. It would advance through the string until the first
counter reaches the string length. Whenever the column counter reaches
the screen limit, a line break is printed, and the column counter is
reset to 0.
Another solution would be to split the string into lines of the allowed
length using the Copy and Delete routines - I don't know if Think
Pascal has these (if not, you could write them).
A good line-break algorithm doesn't cut words in the middle. It looks
something like this:
while the the line is longer than allowed do
begin
Set i to allowed length
If i is *not* just before a blank
begin
From i on, move i backwards until you find a blank
If you don't find a blank, let i be the position of the first
blank in the string
If there is no blank in the string, let i be the length of the
string
end
Print characters 1 to i, let the line be the rest of the characters,
without leading spaces
end
Print the line
Note that this algorithm will not cut lines that have no spaces in
them. Of course, there are more sophisticated algorithms that take
the whole paragraph into account to produce a nicer result. But for a
simple task such as this, the above algorithm should suffice.
- Sebastian