ZIP-to-State, State-to-ZIP

Author(s): Richard Kelly
Publication: Syntax
Pub Details: Syntax/Syntax ZX80 v4 n9 (Volume 4, Issue 9)
Date: September 1983
Pages: 8-10

With lots of memory, many computers can convert a zip into the city and state. With a ZX/TS, you can convert zip to state with a short program.

Here’s the technique we use at SYNTAX to make sure our zip and state match in our mailing list. In sample zip code 48503, the digit 4 determines one of ten national areas and the digits 85 determine the state. All zip codes follow this format. This program stores state abbreviations, and corresponding zip ranges in the arrays S$ and Z$. S$ stores two-letter state abbreviations and corresponding places in Z$ contain lower and upper ranges for the zip.

Note in the following chart the state abbreviation for Michigan (MI) in S$, between Indiana (IN) and Iowa (IA). Number 85 in 48503 lies between the corresponding numbers in Z$ (80 and 99). The two-letter zip code ranges are stored as that number’s character.

S$="...|I |N |M |I |I |A |..."
Z$="...|60|79|80|99|00|28|..."

For each national area, we store the range in array C$. For example, national area 0 is located in Z$ and S$ at positions 1-20. The characters for 1 and 20 are stored in C$ positions 1 and 2.

First, type in the following program, typing 146 X’s in line 1 and 20 X’s in line 3. The X’s hold memory locations that will contain data for zip-state conversions.

   1 LET Z$="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
2 LET S$="UAPRMARINHMEVTCTNJNYNYPADEDCMDVAWVVANCSCGAFLUAALTNMSUAKYUAOHUAINMIIAUAWIMNUASDUANDUAMTILMOUAKSNEUALAUAAROKTXCOUAWYIDUAUTUAAZUANMUANVUACAHIGUORWAAK"
3 LET C$="XXXXXXXXXXXXXXXXXXXX"
10 FOR X=16518 TO 16663
20 INPUT G
30 POKE X,G
40 PRINT G;" ";
50 NEXT X

Now enter GOTO 10. At each prompt, type in one number in the following table (left to right, consecutively). Line 30 POKEs the values for you.

 0   5   6   9  10  27  28  29
30  38  39  49  50  59  60  69
70  89  90  99   0  49  50  96
97  99   0   5   6  19  20  46
47  68  69  69  70  89  90  99
 0  19  20  40  41  49  50  69
70  85  86  97  98  99   0  27
28  29  30  58  59  59  60  79
80  99   0  28  29  29  30  49
50  67  68  69  70  77  78  79
80  88  89  89  90  99   0  29
30  58  59  59  60  79  80  93
94  99   0  14  15  15  16  29
30  49  50  99   0  16  17  19
20  31  32  38  39  39  40  47
48  49  50  65  66  69  70  84
85  89  90  98  99  99   0  66
67  68  69  69  70  79  80  94
95  99

Now change line 10 to read:

  10 FOR X=16832 TO 16851

Type in GOTO 10 then ENTER. Type in the following characters as you did previously.

  1   20   21   26   27   40   41   54
 55   66   67   86   87   98   99  108
109  134  135  146

Now delete the previous lines 10-50 and add the following program, beginning at line 15. The following lines 1-3 show what your lines 1-3 should look like after you POKEd in the previous tables. To RUN the program, type in RUN and choose a menu option (zip-state or state-zip). Then enter either zip or state to see the corresponding match displayed.–RWK

Lines 1-3 as listed after POKEing the tables
Lines 1-3 as listed after POKEing the tables
  15 REM ZIP TO STATE OR RANGE OF ZIP FOR A GIVEN STATE""
16 CLS
20 PRINT AT 0,0;"MAIN MENU "
30 PRINT AT 5,0;"TO GET STATE FROM ZIP---HIT ""S"""
40 PRINT AT 7,0;"TO GET ZIP RANGE FROM STATE ";AT 8,21;"----HIT ""Z"""
45 PRINT AT 10,0;"TO QUIT------------------HIT ""Q"""
50 LET I$=INKEY$
60 IF I$="" THEN GOTO 50
70 IF I$="S" THEN GOTO 500
80 IF I$="Z" THEN GOTO 5000
85 IF I$="Q" THEN GOTO 100
90 GOTO 50
100 CLS
110 PRINT AT 10,10;"BYE"
120 STOP
500 REM ZIP TO STATE.
505 CLS
510 PRINT "TYPE IN THE 1ST 3 DIGITS OF ZIP."
515 INPUT X$
516 IF CODE X$<28 OR CODE X$>37 OR LEN X$<>3 THEN GOTO 541
517 IF VAL X$<006 OR VAL X$>999 THEN GOTO 541
520 LET Z1=VAL X$(1)
525 LET Z2=VAL X$(2 TO 3)
530 FOR X=(CODE C$(Z1*2+1))+1 TO CODE C$(Z1*2+2) STEP 2
535 IF Z2<=CODE Z$(X) THEN GOTO 545
540 NEXT X
541 PRINT "ZIP CODE ERROR, PLEASE TRY AGAIN"
542 GOTO 510
545 LET A$=S$(X-1 TO X)
550 IF A$="UA" THEN PRINT "THAT ZIP CODE IS UNASSIGNED, PLEASE TRY AGAIN.."
560 IF A$="UA" THEN GOTO 510
565 CLS
570 PRINT AT 0,0;"THAT ZIP CODE IS IN THE STATE: ";AT 10,10;"""";A$;""""
580 PRINT AT 13,2;"PRESS ENTER TO GO BACK TO MAIN MENU.."
590 LET I$=INKEY$
600 IF I$=CHR$ 118 THEN GOTO 15
610 GOTO 590
5000 REM STATE TO ZIP
5010 CLS
5020 PRINT "TYPE THE 2-LETTER STATE ABBREV"
5030 INPUT X$
5032 IF CODE X$<38 OR CODE X$>63 OR LEN X$<>2 THEN GOTO 5090
5033 CLS
5035 IF X$="NY" THEN GOTO 5250
5040 FOR X=1 TO 145 STEP 2
5045 IF X$(1)=S$(X) THEN IF X$(2)=S$(X+1) THEN GOTO 5060
5050 NEXT X
5055 GOTO 5090
5060 FOR Y=2 TO 20 STEP 2
5070 IF X<CODE C$(Y) THEN GOTO 5100
5080 NEXT Y
5090 PRINT "STATE ERROR, PLEASE TRY AGAIN"
5095 GOTO 5020
5100 LET Y$=STR$ (Y/2-1)
5110 LET Y$=STR$ (Y/2-1)
5120 LET U$=STR$ (CODE Z$(X))
5130 LET V$=STR$ (CODE Z$(X+1))
5140 IF U$="0" THEN LET U$="00"
5150 IF V$="0" THEN LET V$="00"
5200 PRINT "THE ZIP CODE RANGE FOR ";X$;" IS ";AT 10,10;Y$;U$;"00-";Y$;V$;"99"
5210 GOTO 580
5250 PRINT "THE ZIP CODE RANGE FOR NY IS ";AT 10,10;"09000-14999"
5270 GOTO 580
8999 STOP
SYNTACTIC SUM: 44581, 8K ROM

Artisans bring life to dreams and shape futures by subtle choices of design and execution. Our founding editor, Ann L. Zevnik, led Syntax to excellence, accuracy and utility with her consummate skills. Ann’s influence will remain; Ann will not. We will miss her wit, craft, and knowledge. To those who follow we commend this maxim: if I see farther than others, it is because I stand on the shoulders of giants. Farewell ALZ; find a great dream.

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.