Classic Computer Magazine Archive COMPUTE! ISSUE 73 / JUNE 1986 / PAGE 103

The Beginners Page

Tom R. Halfhill. Editor

More String Arithmetic

We've seen how to slice pieces out of character strings with LEFT$, RIGHT$, and MID$ ("The Beginner's Page," March and April 1986), and even how to use mathematical operators to compare the hidden number values in strings (May 1986). But "string arithmetic" doesn't stop there. BASIC also lets you add two or more strings to make an even longer string.
    In Microsoft BASIC, this is a snap. (Computers with Microsoft or Microsoft-style BASICs include all Commodores, the Amiga, Apple II series, the Macintosh, IBM PC/PCjr, TRS-80, and Atari ST-but not the Atari 400/800, XL, or XE, although Microsoft BASIC is available as an option.) Here's an example:

10 A$="UNITED WE STAND; "
20 B$="DIVIDED WE FALL."
30 C$=A$+B$
40 PRINT A$
50 PRINT B$
60 PRINT C$

The result is:

UNITED WE STAND;
DIVIDED WE FALL.
UNITED WE STAND; DIVIDED WE
 FALL.

    By adding A$+ B$ into a new string variable, C$, we've preserved the original values of A$ and B$. But if this isn't a consideration, you can simply reuse one of the variables:

10 A$="UNITED WE STAND; "
20 B$="DIVIDED WE FALL."
30 A$=A$+B$
40 PRINT A$
50 PRINT B$

The result is:

UNITED WE STAND, DIVIDED WE
  FALL.
DIVIDED WE FALL.

    As you can see, string addition is virtually identical to regular addition: The sum is the whole of the parts. In computerese, the fancy name for this is string concatenation. To impress people, try dropping this term into a conversation at your next user group meeting.
    Although TI BASIC and Extended BASIC for the TI-99/4A are different in many respects from the other versions mentioned above, string concatenation is handled in a similar fashion. The only difference is that the concatenation operator is the & symbol instead of the + symbol. Any of the examples above can be used on the TI by substituting & wherever + appears.

The Fine Print
There's only one string attached when it comes to attaching strings: You have to be careful not to exceed the length limit for strings in your particular version of BASIC. You can hit this limit pretty fast because most Microsoft BASICs don't allow strings longer than 255 characters. An exception is Amiga BASIC, which allows strings up to 32,767 characters long.
    Atari BASIC for the 400/800, XL, and XE computers also allows strings at least 32,000 characters long. As a matter of fact, on an Atari, you'll most likely run out of memory before you hit the length limit on strings. In effect, Atari BASIC lets you set your own length limits. Before using any string variable in an Atari BASIC program, you have to declare its maximum length with a DIM statement. For instance, DIM A$(100) means that A$ can hold up to 100 characters. Since a DIM statement forces Atari BASIC to immediately set aside the specified amount of memory for the string variable, the memory is protected. Nothing else, not even the BASIC program itself, can overwrite it. Many Atari programs take advantage of this megastring feature to reserve huge blocks of memory for holding data files and the like.
    Nothing comes without a price, however, and one price you pay in Atari BASIC is that string concatenation is a little clumsier to write than it is in Microsoft-style BASICs. You can't simply add two strings together with the plus sign. Instead, it requires something like this:

10 DIM A$(50),B$(50),C$(100)
20 A$="UNITED WE STAND; "
30 B$="DIVIDED WE FALL."
40 C$(LEN(C$)+1)=A$
50 C$(LEN(C$)+1)=B$
60 PRINT A$
70 PRINT B$
80 PRINT C$

The result is:

UNITED WE STAND;
DIVIDED WE FALL.
UNITED WE STAND; DIVIDED WE
 FALL.

    It takes two statements (lines 40 and 50) to accomplish the equivalent of C$=A$+B$ in Microsoft BASIC. Essentially, what these lines do is use the LEN function to say, "Starting at the last character in C$ plus one, append the contents of A$; then, starting at the last character in the new C$ plus one, append the contents of B$." Although not as readable or as easy to use as C$=A$+B$, the result is exactly the same.
    If you're not interested in preserving the original contents of A$ and B$, it is possible to concatenate in one line. Substitute this statement and delete lines 50 and 80:

40 A$(LEN(A$)+1)=B$

The result is:

UNITED WE STAND; DIVIDED WE
 FALL.
DIVIDED WE FALL.

    In either Microsoft or Atari BASIC, there's no such thing as string subtraction, multiplication, or division with the -, *, and / signs. Instead, you have to simulate these operations by slicing up the string with LEFT$, RIGHT$, MID$, and the other methods shown in the past few columns.