Page 1 of 1

ZX80 4K ROM string length

Posted: Sun Jan 28, 2018 5:09 pm
by Shaun_B
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.

Re: ZX80 4K ROM string length

Posted: Sun Jan 28, 2018 6:07 pm
by Shaun_B
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.