Anyone who uses a wordprocessing program has done it. Greeting card programs even do it. But what if I want to do it in my own programs?
If you are not lost yet, what I am speaking of is LEFT, CENTER, and RIGHT Justification of text lines on a screen display or on a hard copy printout. Left justification is when the first character of each line of text in a paragraph lines up evenly across the left margin of the screen or page. For example, this paragraph is left justified.
Center justification is placing the text line, headline or paragraph in the middle of the screen or page, like this paragraph.
Center justification is just placing the text line, headline or paragraph in the middle of the screen or page, like this paragraph.
The other evening I found that I needed some routines that would allow me to left, center, and right justify a string (t$) of text for display. The three routineds which are discussed here accomplish this by making a copy of the text string and then placing each character back into the original string in the new format (see Listing B).
10 REM Justify It
20 REM by Mike Felerski
30 LET max=32
40 DIM t$(max)
50 DIM o$(max)
60 DIM r$(32)
70 POKE 23658,8
100 GO SUB 300
110 LET t$=r$
112 PRINT AT 14,5;"(L)eft"
120 PRINT AT 15,5;"(C)enter"
130 PRINT AT 16,5;"(R)ight"
140 INPUT "Choice? ";c$
150 IF c$="L" THEN GO SUB 400
0: GO SUB 200: GO TO 110
160 IF c$="C" THEN GO SUB 4100
: GO SUB 200: GO TO 110
170 IF c$="R" THEN GO SUB 4200
: GO SUB 200: GO TO 110
180 GO TO 140
200 REM Clear and Print
205 PRINT AT 8,0;"54321 9876543
2100123456789 12345"
210 PRINT AT 10,0;t$: RETURN
300 REM Enter String
310 PRINT AT 20,0;"Enter String
";max;" Chars Max"
320 INPUT r$: LET t$=r$: GO SUB
200: RETURN
Listing A
The routines assume that the text string is stored in t$ and that the size of both t$ and o$ are DIMensioned to max. o$ is used as the temporary string. Listing A is a demo/main routine that let us enter, justify, and re-display it.
4000 REM Left Justify
4005 LET o$=t$: LET pos=0
4010 IF o$(pos+1)=CHR$ 32 THEN
LET pos=pos+1: GO TO 4010
4020 IF pos=0 THEN RETURN
4030 FOR x=pos+1 TO max: LET t$(
1)=o$(x): LET i=i+1: NEXT x
4040 FOR x=1 TO max: LET t$(x)=C
HR$ 32: NEXT x
4050 RETURN
4100 REM Center Justify
4110 GO SUB 4000: LET o$=t$: LET
pos=max
4120 IF o$(pos)<>CHR$ 32 THEN G
O TO 4150
4130 LET pos=pos-1: IF pos=0 THE
N GO TO 4150
4140 GO TO 4120
4150 LET pos=(INT ((max-pos)/2)+
1)
4160 FOR x=1 TO pos-1: LET t$(x)
=CHR$ 32: NEXT x
4170 LET i=1
4180 FOR x=pos TO max: LET t$(x)
=o$(i): LET i=i+1: NEXT x
4199 RETURN
Listing B
Once the listings A, B, and C are typed in, RUN the program, enter some text, and then test the options to see the routines at work. If you are using ZEBRA SYSTEMS OS64, then try replacing line 30 with LET max=64. You may also replace the PRINT statement in line 210 with an LPRINT.
4200 REM Right Justify
4205 LET o$=t$: LET pos=max+1
4220 IF o$(pos-1)<>CHR$ 32 THEN
GO TO 4230
4222 LET pos=pos-1
4225 IF pos=0 THEN RETURN
4227 GO TO 4220
4230 LET i=max
4235 FOR x=pos-1 TO 1 STEP -1: L
ET t$(i)=o$(x): LET i=i-1: NEXT
x
4240 FOR x=i TO 1 STEP -1: LET t
$(x)=CHR$ 32: NEXT x
4250 RETURN
Listing C
The theory behind the routines is to find the first non-blank character in the string whose position is placed in pos. We look left to right, to left justify; and right to left, to right justify. This is the point at which we start to pick out each character and place them from o$ into t$, starting at t$(1) or t$(max) for left or right justification respectively.
In order to center the string, we first left justify the string, using a GOSUB 4000 so there is less guess work as to where the first non blank character is. Then we count back through the string, looking for the first non-blank character, starting from position max. We then take this value (pos), divide it by two and add 1 (adding 1 is optional per programmer’s taste). This then gives us the starting lefthand position within the string. Finally the characters are picked and placed just as in the left and right routines.
Retyped from the I.S.T.U.G newsletter,.by GFC (with minor revisions to the listings)
Media
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.