Authors
Publication
Pub Details
Date
Pages
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 hardcopy printout. Left justification is when the first character of each line of text lines up evenly along the left margin of the screen or page. For instance, this paragraph is left justified.
This paragraph, on the other hand, is right justified, where all of the last characters in the text lines align along the right margin.
Center justification is just placing the text line, headline or paragraph in the middle of the screen or page.
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 routines 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 the second listing).
10 REM Justify It
20 REM by Mike Felerski
30 LET max=32
40 DIM t$(max)
50 DIM o$(max)
100 GOSUB 300
110 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 GOSUB 4000: GOSUB 200: GOTO 110
160 IF c$="C" THEN GOSUB 4100: GOSUB 200: GOTO 110
170 IF c$="R" THEN GOSUB 4200: GOSUB 200: GOTO 110
180 GOTO 140
200 REM Clear and Print
205 CLS: PRINT AT 8,0;"01234567890123456789012"
210 PRINT AT 10,0;t$: RETURN
300 REM Enter String
310 PRINT AT 20,0;"Enter string ";max;" Chars Max"
320 INPUT t$: GOSUB 200: RETURN
The routines assume that the text string is stored in t$ and that the size of both t$ and o$ is DIMensioned to max. o$ is used as a temporary string.
The listing above is a demo/main routine that lets us enter, justify and re-display it.
4000 REM Left justify
4005 LET o$=t$: LET pos=0
4010 IF i$(pos+1)=CHR$ 32 THEN LET pos=pos+1: GOTO 4010
4020 IF pos=0 THEN RETURN
4025 LET i=1
4030 FOR x=pos+1 to max: LET t$(i)=o$(x): LET i=i+1: NEXT x
4040 FOR x=i to max: LET t$(x)=CHR$ 32: NEXT x
4050 RETURN
4100 REM Center Justify
4110 GOSUB 4000: LET o$=t$: LET pos=max
4120 IF t$(pos)<>CHR$ 32 THEN GOTO 4150
4130 LET pos=pos-1: IF pos=0 THEN GOTO 4150
4140 GOTO 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=post TO max: LET t$(x)=o$(i): LET i=i+1: NEXT x
4199 RETURN
Once all three listings 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 Right Justify
4205 LET o$=t$: LET pos=max+1
4220 IF o$(pos-1)<>CHR$ 32 THEN GOTO 4230
4222 LET pos=pos-1
4225 IF pos=0 THEN RETURN
4227 GOTO 4220
4230 LET i=max
4235 FOR x=pos-1 TO 1 STEP -1: LET 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
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 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 respectfully.
In order to center the string, we first left justify the entire string using GOSUB 4000, so there is less guess work as to where the first non blank character is. Then we count back through the blank characters, starting from position max. We then take this value (pos) and divide it by two and add 1 (adding 1 is optional per programmer’s taste). This then gives us the starting left hand position within the string. Finally, the characters are picked and placed just as in the left and right routines.
This may not be the fastest method (there is always machine code) and it may not be the most compact way to justify a string, but it is fairly universal and can be ported to TS1000 and QL computers without much modification.
Products
Downloadable Media
Image Gallery
Source Code
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.