ZX80 4K ROM string length

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Post Reply
Shaun_B
Posts: 410
Joined: Wed Apr 22, 2009 10:22 am

ZX80 4K ROM string length

Post by Shaun_B » Sun Jan 28, 2018 5:09 pm

Here's a sub routine that will count the length of a string (including example).

Code: Select all

     10 LET A$="HOW LONG IS A PIECE OF STRING?"
     20 LET B$=A$
     30 GOSUB 500
     40 PRINT "LENGTH IS ";L
    100 STOP
    500 REM STRING LENGTH
    510  LET L=0
    520  IF B$="" THEN GO TO 560
    530   LET L=L+1
    540   LET B$=TL$(B$)
    550  GO TO 520
    560 RETURN
 
The B$ variable is used as a buffer (as ZX80 4K ROM BASIC doesn't have the same string handling routines as the 'New ROM') as the original string (A$) is being reduced by one character in the sub routine (line 540).

Regards,

Shaun.

Shaun_B
Posts: 410
Joined: Wed Apr 22, 2009 10:22 am

Re: ZX80 4K ROM string length

Post by Shaun_B » Sun Jan 28, 2018 6:07 pm

You can add to the routine above a string reverse routine as follows:

Code: Select all

     600 REM GNIRTS ESREVER
     610  LET R=0
     620  IF R=L THEN RETURN
     630  LET R=L
     640  IF R<0 THEN RETURN
     650  FOR I=0 TO R
     660   LET B$=TL$(B$)
     670  NEXT I
     680  PRINT CHR$(CODE(B$));
     690  LET R=R-1
     700  LET B$=R$
     710 GO TO 640
Adding to the string length listing, it will be used like this:

Code: Select all

     40 LET B$=A$
     50 GO SUB 600
     60 PRINT CHR$(CODE(B$))
Regards,

Shaun.

Post Reply