!lm12
!rm75
Splitting Strings to Fit Your Display......Bob Sander-Cederlof

Printing text on the screen, or even on a printer, is not as easy at it ought to be.  The problem is splitting words at the right margin.  Word processors handle it nicely, but what do you do in an Applesoft program?

You might write a subroutine, in Applesoft, which looks for the first space between words before a specified column position.  The subroutine could split a string at the space into two substrings:  one containing the next display line, the other the remainder of the original string.

You might.  But believe me, it builds up a lot of garbage strings and takes a long time to execute.  If you like the general approach, you might try coding the subroutine in assembly language.  You can avoid garbage build-up and save lots of running time, so it is probably worth the effort.  Especially since I already wrote the program for you!

The program is written to be called from an ampersand parser like the one in last month's article on REPEAT/UNTIL.  Or, you can use it with Amper-Magic, Amperware, The Routine Machine, etc.  It is fully relocatable, having no internal data or JMP/JSR addresses.  I set the origin to $300, but it can be loaded and used anywhere without re-assembly.

Here is an Applesoft program to show how to call SPLIT:

!lm+5
100  POKE 1014,0: POKE 1015,3
120  FOR N = 5 TO 40 STEP 3: GOSUB 1000: NEXT : END 
1000 A$ = "NOW IS THE TIME FOR ALL GOOD MEN TO COME
     TO THE AID OF THEIR PARTY."
1005  & ,A$,B$,N
1010  PRINT B$
1020  IF A$ <  > "" THEN 1005
1025  PRINT 
1030  RETURN 
!lm-5

Call SPLIT with three parameters.  The first (A$ above) is the source string, which will be split.  After SPLITting, the remainder string will be left in A$.

The second parameter, B$ above, will receive the left part, including the last complete word, up to N (the 3rd parameter) characters.  If there is no space in the left N characters, exactly N characters will be split.

Here are some of the printouts from the test program:

!lm+5
N=5            N=11            N=20
-----          -----------     --------------------
NOW            NOW IS THE      NOW IS THE TIME FOR
IS             TIME FOR        ALL GOOD MEN TO COME
THE            ALL GOOD        TO THE AID OF THEIR
TIME           MEN TO COME     PARTY.
...etc.        ...etc.
