How to 'manipulate' strings in BASIC V2?

509 Views Asked by At

I would like to reach the following: I ask for a number from the user, and then output a string like the following:

-STR$

--STR$

---STR$

----STR$

-----STR$

I tried to do this:

10 INPUT NUM%
20 FOR X=1 TO NUM%: PRINT NUM%*"-" + "TEXT" : NEXT

The code above got me an error: ?TYPE MISMATCH EROR IN 20

However, I didn't yet figure out how to manipulate the string's beginning to multiply the '-' marks on each loop run

3

There are 3 best solutions below

2
MiniMik On BEST ANSWER

Maybe this:

10 INPUT NUM%
20 FOR I = 1 TO NUM%
30 FOR J = 1 TO I: PRINT "-"; : NEXT
40 PRINT " TEXT"
50 NEXT

There is no multipy of strings/character, as far as I remember to old (good) times.

3
Bill Hileman On

I believe even older, more primitive forms of BASIC had the STRING$() function. It takes two parameters: the number of times to repeat the character and the character itself. So...

10 INPUT NUM%
20 FOR X=1 TO NUM%: PRINT STRING$(NUM%, "-") + "TEXT" : NEXT
2
tendim On

An alternative:

100 INPUT NM%
110 BR$="----------"
120 PRINT LEFT$(BR$,NM%);
130 PRINT "TEXT"

This eliminates the need for an expensive FOR loop, and should be okay as long as NM% is not greater than the length of BR$.

One other thing to point out is that your variable names are effectively capped at two characters, e.g.:

The length of variable names are optional, but max. 80 chars (logical input line of BASIC). The BASIC interpreter used only the first 2 chars for controlling the using variables. The variables A$ and AA$ are different, but not AB$ and ABC$.

(Source: https://www.c64-wiki.com/wiki/Variable). For that reason I used NM% instead of NUM%; it will prevent issues later.