Beginners Series Part 8

Cursor codes, loops and triangles of asteriks in our series to help new users.

Volume 1

Number 10

December 1983

Doing things with strings

LET'S start off this month by reviewing the difference between string and numeric variables.

String variables label "strings" of characters, such as "HELLO" and "GOODBYE".

Numeric variables label numbers in a way that lets us do sums with them.

We can use whole words for both types of labels.

You tell the computer it's dealing with a string variable by ending the label with a $ symbol.

This means that word$ would label a string, while number labels a number.

We also met another type of numeric variable, the integer variable. In this case the label is followed by a % sign. Integer variables deal with whole, or integer numbers - they haven't learnt to do decimals!

Now the computer can remember a number in the form of a string variable, in the same way as it can remember any string of characters.

This means that: number$="123" makes sense. Enter it now. (Remember that when we assign a string variable, the string of characters must be in quotes.)

If you now try: PRINT number$ you will indeed get 123 appearing on the screen.

So you can, if you wish, label numbers as strings.

You cannot, though, do sums with the strings. Try: PRINT 2 * number$ and see what happens!

The moral is, if you want to do sums stick to numeric variables.

Sometimes, however, for reasons we won't go into for a while, numbers you want to work with may be stored in string form.

Suppose we want to do sums with the 123 we stored in number$. We need some way to convert it into a numeric form. We do this with VAL( ) - think of it as standing for "value".

You put the string you want converting inside the brackets. Try: number=VAL("123") followed by: PRINT 2 * number This should prove to you that the string really has been converted into a number! Notice that there are two variables here, number and number$. The former is numeric, the latter string. Of course, you can put the string variable inside the brackets rather than the full string. So you can use number$ rather than "123". Enter: number=VAL(number$) then: PRINT 2 * number and you'll see that it works just as well. There's also a way to convert numeric variables into strings. Again you might be wondering why anyone would bother, but for the moment, just take it on trust that one day you'll want to! We use the function STR$( ), which you can consider as standing for 'strings'. Enter: number=123 followed by: number$=STR$(number) PRINT number$ and you'll see how it works.

Now when I said that you can't do sums with strings I was being completely truthful. However there is something you can do with strings that looks suspiciously like you're doing sums.

Enter: string$="*"+"@" PRINT string$

What happens is that the ' + ' sign 'glues' the two strings together. The jargon calls this "concatenating strings". The add sign in this case doesn't mean addition. Try: string$="123"+"l23" PRINT string$ and you'll see what I mean.

Sometimes it's useful for us to know how long a string is - after all, the computer sometimes receives strings via an INPUT statement and you won't know its length.

We use the function LEN( ), short for length. Try the following: string$="ABCDEF" PRINT LEN (string$) Also try the above with a couple of spaces between the first quotes and the A - spaces count!

Have a look at Program I.

10 REM *** PROGRAM I ***
20 MODE 6
30 first$="ABC"+" "+"DEF"
40 second$="ABC"+""+"DEF"
50 PRINT ''''"first$ is "first$
60 PRINT "Its length is "; LEN(first$)
70 PRINT "second$ is "second$ 80 PRINT "Its length is "; LEN(second$)
Program I

Look carefully at lines 30 and 40. The strings first$ and second$ are not identical. In line 30 the second pair of quotes encloses a space. In line 40, the second pair of quotes enclose absolutely nothing - not even a space.

We say that they enclose the "null string" that is, absolutely nothing.

This results in there being a space between the ABC and DEF in first$, but there being absolutely nothing between the two sets of characters in second$.

It also explains the different lengths.

Have a look at Program II. Notice, by the way, that lines 50 to 100 contain a lot of repetitions -judicious use of the copy key will save you a lot of typing!

10 REM *** PROSRAM II ***
20 MODE 6
30 string$="*"
40 PRINT '''string$
50 string$=string$+"*"
60 PRINT string$
70 string$=string$+"*"
80 PRINT string$
90 string$=string$+"*"
100 PRINT string$
Program II

Line 30 sets string$ to one asterisk and prints it in line 40.

Line 50 then adds an asterisk onto the end of string$, making it two asterisks long. It is then printed in line 60.

Line 70 similarly glues an asterisk on the end, line 80 then prints the whole string, and so on.

In describing the above program there was a "dead giveaway": I said that it contained "a lot of repetitions". If that's so, why didn't I use a loop? I rectify this in program HI.

10 REM *** PROGRAM III ***
20 MODE 6
30 INPUT '''"How many times", number%
40 FOR loop%=1 TO nunber%
50 string$=string$+"*"
60 PRINT string$
70 NEXT loop%
Program III

This is the first time we've actually INPUT the loop parameter, in this case number%. This means that we leave specifying how many times we perform the loop until the program is actually running!

Line 50 contains the heart of the program. Each time through the loop it increases string$ by one asterisk then prints it out. The result's a neat looking triangle of asterisks!

Try entering low values for number% at first. What happens if you enter 0?

Now that we understand the LEN( ) function, we can achieve the same results as Program III with a REPEAT... UNTIL loop. Look at Program IV.

10 REM ** PROGRAM IV ***
20 MODE 6
30 INPUT '''"How many time$",number%
50 string$=string$+"*"
60 PRINT string$
70 UNTIL LEN(string$)=number%
Program IV

Here we simply repeat until the length of string$ has reached number% (The length of string$ increases by one each time through the loop so we can use it to keep track of how many times we've "looped".)

Try Program V.

10 REM *** PROGRAM V ***
20 MODE 0
30 offset%=64
40 FOR loop%=1 TO 26
50 string$ = string$ + CHR$(offset% + loop%)
60 PRINT string$
70 NEXT loop%
Program V

This uses the same idea as Programs III and IV. Line 50 simply increases the string by one character each time through the loop. The character is determined by the CHR$( ) function. The first time through the loop it's 'A', the second time a 'B' is glued onto the end of the 'A' and so on until we eventually have the whole alphabet.

Notice that line 20 puts the micro into Mode 0. This is so we can fit it all on the screen! You might want to return your micro to normal after the program with a MODE 6.

See if you can predict what happens if you changed line 50 to:

50 string$ = CHR$(offset% + loop%) + string$

Finally, can you alter program V so that it still prints out its "alphabetic triangle", but with the alphabet reversed? That is, so that it goes: