Classic Computer Magazine Archive ANTIC VOL. 5, NO. 9 / JANUARY 1987

BY DAVID PLOTKIN, ANTIC CONTRIBUTING EDITOR

This series started in the March, 1986 issue and teaches beginners how to program in BASIC on all Atari 8-bit computers such as the 800XL and the 130XE.

New Owners Column

Lesson 10: Strings


While most of us see a string as something to attach to a kite, your Atari takes a very different view. This month we'll discuss setting up and manipulating strings, also some of their uses.

A string can be a series of letters, numbers or symbols-a string of characters. "*** Hello there world ***" is one example of a string. In fact, almost anything that you can type into your computer and enclose in quotation marks ("") can be a string. When you type a string into your computer, it is called a string constant, and it can be used in many ways, such as PRINT statements:

10 PRINT "****HELLO THERE WORLD***"

You've seen many examples of string constants in previous New Owner's Columns. But string constants are not where the real power of strings lie.

STRING VARIABLES

The real power lies in string variables. As the name implies, the contents of the string can be varied, so custom strings can be built from within the program. The first step to using string variables is to declare them. String variable names can be almost any length, but you can't use reserved BASIC keywords for variable names. And they must always have a dollar sign at the end: A$, RULE$, and REALLYLONGNAME$ are all valid string variables. A string variable such as B$ is read as "B-string".

Before using a string variable in your program, you must DIMension it, just as with the subscripted variables discussed last month in the December, 1986 Antic. The form is the same:

10 DIM A$(100),RULE$(l0)

You can DIMension subscripted variables in the same DIM statement with string variables. The number in parenthesis following the string variable name is the maximum length of the variable. In this case, Attempting to put more than 100 characters in A$ generates an error.

After your string variables are DIMensioned, they can be used in various ways. One way to put information into a string is to use the INPUT statement:

10 DIM A$(20)
20 PRINT "WHAT IS YOUR NAME":INPUT A$
30 PRINT "YOUR NAME IS ";A$

This short program asks for your name. When you type your name on the keyboard, the information is put into A$. Note that if you type in a name longer than 20 characters (the DIMensioned length of the string), only the first 20 characters will be stored in A$. The rest will be lost. Try it and see.

Another way to put information into a string is with READ/DATA statements. DATA statements may contain strings which can be READ:

10 DIM NAME$(20)
20 READ NAME$:PRINT NAME$
30 DATA DAVID PLOTKIN

This program reads the DATA in line 30 into NAME$. Note that your Atari will read DAVID PLOTKIN as just one item of DATA, while DAVID, PLOTKIN would be two DATA items because of separating comma (,).

LEN STATEMENTS

After information is stored in a string, you can manipulate that information within the string and also transfer it to other strings. The first thing you'll normally need to know about a string is the current length, which is given by the LEN statement:

100 A=LEN(NAME$)

LEN does not give the maximum length of the string as stated in the DIM statement, but rather the number of characters currently in the string. For example, if you DIMension NAME$ to 20, and the user only INPUTs "DAVID", then the length of the string as returned by LEN is 5. If no information has yet been entered into the string, then LEN will return a length of 0. We'll see shortly why LEN is so important.

The simplest way to move information either inside a string or between strings is to set two strings equal:

10 DIM A$(20),B$(30)
20 A$="HELLO THERE READER"
30 B$=A$:PRINT B$

The contents of A$, as set up in line 20, are entered into B$ by line 30. Note that AS still contains its original contents, so the two strings are now equal, just as you'd expect.

SUBSTRINGS AND SUBSCRIPTS

The other way to move information around is with portions of strings, called substrings. To access a substring, you specify which element(s) of the string you want:

10 DIM A$(20),B$(30):A$="HELLO THERE READER"
20 B$=A$(l,5):PRINT B$

Line 20's two numbers in parenthesis (1,5) are called subscripts. The first represents the starting point of the string and the second is the ending point. In this example, B$ is equal to the elements 1-5 of A$. If no second subscript is supplied, then the substring is considered to go from the first (only) subscript to the end of the string. If the subscript is 1, this effectively sets the two strings equal. You can equate substrings of both strings:

40 B$(3,5)=A$(6,8):PRINT B$(3,5)

As shown above, you can PRINT substrings. Note that strange results may occur if the length of the two equated substrings are not equal. Whichever substring is smaller will govern the amount of information transferred:

40 B$(1,10)=A$(2,2)

The above line puts the character in position 2 of A$ into position 1 in BS, but the rest of B$ is not affected.
You can use the substring techniques to make strings longer. This is where the LEN function is important. Since LEN returns the current length of the string, you may use it to ensure that new strings and substrings are added to the end of the current string. For example, consider a program that requires the user to input a file name to be read from disk. As you know, disk file names must be preceded by "D:" or "Dn:" where n is a digit from 1 to 8. But what if the user forgets the "D:"? The example builds a new string from the filename, adding "D:" if it is absent:

10 DIM FILENAME$(16),NAME$(16)
20 PRINT "WHAT FILE NAME":INPUT NAME$
30 IF NAME$="" THEN GOTO 20:REM JUST IN CASE THE USER JUST HITS [RETURN]
40 IF NAME$(1,l)="D" AND NAME$(2,2)=":" OR NAME$(3,3)":" THEN FILENAME$=NAME$:GOTO 100
50 REM TEST FOR "Dn:" OR "D:". SKIP OVER THE REST IF IT IS PRESENT
60 FILENAME$(1,2)"D:":FILENAMES (LEN(FILENAME$+1))=NAME$
100 PRINT FILENAME$

In line 60, we build the string we will need. Note that the use of the LEN function tells us how long our string FILENAME$ is. By building our substring starting at one more than the current length of the string, we can tack on information without disturbing what's already there. Note that in this example, we actually knew how long FILENAME$ was, but there will be times when we won't know. Notice line 30 also: if NAME$ is empty, the statement NAME$="" (two quotation marks with no spaces between) is true. You can erase all the information in a string by using this same form of statement:

30 NAMES="":REM clears the string.

CONVERTING STRINGS

The function VAL converts strings to numbers, and STR$ converts numbers to strings. VAL works by changing any numbers contained in a string to their numeric value:

30 N=VAL(A$)

If the first character of the string contained in the VAL statement is a letter, you'll get an error. VAL stops at the first non-numeric character, ignoring the rest of the string. VAL can be used on a substring or a string constant.

STR$ is useful if you need to put a number into a part of a string:

10 DIM A$:N=123:A$(1,5)="AAAAB":A$(6)=STR$(N) Puts "123" into positions 6-8 of A$.

COMPARING STRINGS

Two or more strings are equal when they are identical. What could be simpler? Thus if A$="AAA" and B$= "AAA" then the statement "A$ =B$" is true. But the concept of strings having comparative value needs some explanation. Each character in a string is represented by a value in your computer called the ATASCII value. (ATASCII will be the subject of next month's column). For example, the ATASCII code for the letter A is 65. You'll find a table of these codes in the back of your manual.

Strings are compared, based on these codes, character by character. If only the first letter of each string is considered, then the string beginning with the character having the lower value will be considered to be the smaller string. That is, if A$="BCD" and B$="CDE", then A$<B$, because the letter B at the beginning of A$ has a code of 66 and the letter C at the beginning of B$ has a code of 67. If the letter at the beginning of each string is the same, then the comparison continues until two different letters are found.

THIS MONTH'S LISTING

This month we present a simulation-our longest New Owners Column program yet. If you have ever wanted to play the stock market without endangering your life savings, here's your chance. This program also demonstrates the use of strings. Instructions follow. I've made quite a bit in this stock market-and also lost my shirt.

To enter the market, type in Listing 1, NEWOWN9.BAS, check it with TYPO II and SAVE a copy before you RUN it.

In this stock market simulation you must buy and sell the available stocks, using information from the newspaper headlines. You start with $10,000 and you must try to increase the value of your holdings as much as you can in six buying/selling sessions.

Read the headlines and try to determine how they will affect your stocks. Some of the headlines are misleading, so take what you read with a grain of salt. The companies you'll trade in are: Shay-Dee Petroleum, Lemon Computers, Weapons 'R' Us, Admiral Motors, Hottenkold Gas & Electric and Worldwide Communications Network.

Listing 1: NEWOWN10.BAS Download



Please note that last month's column skipped a lesson number The December, 1986 column should have been numbered lesson 8 instead of lesson 9. The mistake arose during production of the issue, due to confusion over whether The Great GOTO Debate (October, 1986) was a New Owners lesson or not. (It wasn't!) In hopes of minimizing further confusion, we'll call the column in this issue lesson 10 and continue on from there. Please be assured that no lessons have been left out of this popular series.- ANTIC ED