The Computer Tutor: String Manipulation

Author(s): Gene G. Buza
Publication: Synchro-Sette
Pub Details: Synchro-Sette v1 n8 (Volume 1, Issue 8)
Date: November 1982
Pages: 10-15
Computer Tutor illustration, tutor in mortarboard pointing at a computer
Computer Tutor illustration, tutor in mortarboard pointing at a computer

Good afternoon class. I see you’re all looking fit today! Today’s subject will be on string manipulation.

Can anyone give me an example of string manipulation? Yes, the gentleman in the rear has his hand up.

No, that’s wrong – and please put that yo-yo away. That’s not the kind of string we’re discussing here today.

No, the strings I’m interested in are string variables in BASIC computer programs, such as the BULLETIN BOARD programs and the HOME BUDGET programs. Let me ask another question. What would be the minimum amount of program lines needed to allow a MARQUEE type message scroll across the screen on the Sinclair computer?

One person says 20 – another 15 – still another 13.

Would you believe it could be done in five? That’s right, five. ENTER the following program into your computer:

SCROLLING DEMO PROGRAM

10 INPUT A$
20 LET A$ = " 32 blank spaces "
+ A$ + " "
30 PRINT AT 10, 0; A$( TO 32 )
40 LET A$ = A$(2 TO LEN A$) +
A$(1)
50 GOTO 30

RUN the program and ENTER any message you want or as much as your computer will hold. Then press the ENTER key. The appearance of movement of the message occurs, but in actuality, a string of 32 characters is constantly being thrown on the screen, changed, thrown again on the screen, changed, etc.

The message is not really moving but only gives the illusion of movement. We can get the same illusion in the opposite direction by changing line 40 to read:

40 LET A$ = A$(LEN A$) + A$( TO
LEN A$ -1)

Instead of RUNning the program again and retyping in the message, just ENTER the command – GOTO 30. Of course, we are not limited to just alpha-numeric character entry. We can also introduce graphic characters to produce some pretty startling effects. The beginning of last month’s cassette program “BULLETIN” used two of these routines to intoduce the Bulletin Board.

Pretty nifty, huh? This is one method we can use to manipulate string variables.

There are others, though, such as word processing. Let us say that we had a word processor such as the 1K “MICROPRO” on last month’s cassette. This program created one variable that could be increased or decreased in length.

But, what if we wanted to insert a word into that string variable? Enter the following simple program:

WORD PROCESSING DEMO PROGRAM

10 LET B$ = ""
20 LET C$ = " "
100 LET A$ = INKEY$
120 IF CODE A$ = 114 THEN
GOSUB 300
130 IF CODE A$ = 115 THEN
GOSUB 400
140 IF CODE A$ = 23 THEN
LET A$ = " "
150 IF CODE A$ = 228 THEN
GOSUB 500
190 IF CODE A$>63 THEN
LET A$ = ""
200 LET B$ = B$ + A$
210 PRINT AT 11,0 ; B$; AT 12,
LEN B$; CHR$ 136; AT 12,
LEN B$; " "; AT 11,LEN B$; C$
220 GOTO 100
300 LET C$ = B$(LEN B$) + C$
310 LET B$ = B$(TO LEN B$ -1)
320 LET A$ =""
330 RETURN
400 LET B$ = B$ + C$(1)
410 LET C$ = C$(2 TO LEN C$)
420 LET A$ = ""
430 RETURN
500 LET C$ = C$(2 TO LEN C$)
510 LET A$ = ""
520 RETURN

RUN this program and enter the following sample text one character at a time.

“HELLO, HOW ARE YOU?”

This program will only allow one line of text to be entered on the screen. The blinking cursor appears under the next character position to be inputted.

CURSOR LEFT & RIGHT – The SHIFT arrows allow the cursor to move left or right through the text. You will notice that if you hold any character key down, the character will repeat itself.

DELETE – To delete a character, press the SHIFT key and the (D) key.

BLANK SPACE – To put a blank space in, press the SHIFT key and the asterisk (*) key. CAUTION – Do not press BREAK or SHIFT RIGHT ARROW for a blank space.

INSERTION – To insert text into existing text, just place the cursor under the position of the text where you want the text to be inserted. An example would be under the (A) in (ARE) and type in “THE HECK “. See how easy it is?

This is all done by manipulating the string variables, (B$) and (C$) with the (TO) function. For you more experienced programmers it should be a good exercise to try to combine these techniques with some of the other word-processing routines and programs from our past issues to produce a word-processor that can hold many lines of text.

As you can see from these examples, the (TO) function is an extremely powerful string manipulating device. Not only is it powerful but also extremely fast, even in the SLOW mode. Most word processors have to be written in machine language in order to get the speed necessary to make them workable. It is my belief that because of the speed and power of the (TO) function, a very fast word processing program could be written in BASIC that would have most if not all of the features of established programs for other microcomputers, such as the ELECTRIC PENCIL and SCRIPTSIT programs. These other programs can sell for $100 and more.

But be that as it may, let us move on to other uses for string manipulation.

DATA FORMATTING

In past issues and from cassette programs, we have learned that if we wanted to create a list of data, such as a customer file, we would have to have separate variable arrays for every item category to be inputted. The man in the rear has his hand up again. Yes sir?

He wants to know what a variable array is. Good question.

ARRAYS

An array is a word used to format a category of variable information. Let us say that we took our little address book that we all have either on our person or at home and we wanted to put all those names, addresses, phone numbers and other related information into the computer’s memory. Let us say that we never expected the list to exceed 100 people. In the FILE program on the OCT/82 cassette, we find this is accomplished by dimensionalizing out a string variable for each item to be entered using the format DIM N$(100,n) where (N$) would be the string variable assigned to the particular item entered, the (100) is the maximum amount of those items that will be entered and (n) would be a number that would represent the maximum amount of characters in that item. We now have the format set for the creation of an array to allow the input of a maximum of 100 items. Let us say that this array is for last names.

But, what if we needed more than one set of items, such as another array the input of a maximum of 100 first names and other separate arrays for addresses, phone numbers, cities or towns, zip codes, etc. We would need to dimensionalize more string variables, one for each category of items that we have and using up one letter of the alphabet for each category.

Can anyone tell us what the limit would be to the amount of arrays we can have in a program? Yes, the gentleman in the rear has his hand up again?

He says that after the 26 letters of the alphabet are used up, no more arrays can be created.

Once again, your answer is wrong, sir, but I can easily understand how you came to that conclusion. There is another method which allows you as many variables as the computer’s memory will allow, but first let me discuss some of the methods used on some of the more expensive computers with disk drives.

SEQUENTIAL VS. RANDOM ACCESS DATA STORAGE

Data storage and retrieval from disk can be done with two different methods. Sequential storage and retrieval requires all data to be in the computer’s memory banks as it is being processed. This means that in a name and address program, all the name and address information is in the computer and is available for recall from the computer’s memory chips.

Random Access data storage works on an entirely different principal. The total data is never entirely in the computer’s memory banks – only the data which is pertinent to the operation of that part of the program. The program allows the data to be inputted and then stores it on the disk. The program then forgets the data and moves onto the next input. When the data is to be retrieved, it is displayed on the CRT or printed onto paper and then forgotten. Sometimes routines in the program exist to total numeric values.

The advantage of Random Access storage is that the disk storage media can hold a lot more data than the computer’s memory can, the advantage being maybe 10, 20 or 100 times as much. The disadvantage is that program totaling routines require the disc drive to always be running so that it is searching for the required data and is therefore more time-consuming. This is particularly true for sort routines that require disk variable swapping.

You are probably wondering what any of this has to do with more than 26 variable arrays being available for the Sinclair computer. The reason I brought all this up is because of the methods used for Random Access storage where a whole bunch of string variables can be stored in one main string variable.

FIELDS, ELEMENTS AND SUB-VARIABLES

ENTER the following program:

10 CLS
20 IF LEN A$>9 THEN GOTO 5
30 PRINT ,, A$
40 FOR N = 1 TO LEN A$
50 PRINT N;
60 NEXT N
70 PRINT "FIRST NUMBER?"
80 INPUT A
90 IF A<1 OR A> LEN A$ THEN
GOTO 80
100 PRINT ,,,, "LAST NUMBER?"
110 INPUT B
120 IF B< LEN A$ - A THEN
GOTO 110
130 PRINT ,,,, A$(A TO B)
140 GOTO 5

RUN this program and ENTER a string of characters no more than 9 characters in length. After you press the ENTER key, you will see a number under each character corresponding with that character’s position in the string. The program will now allow you to break out a segment of that string from the first number entered to the last number entered. The program will recycle so that you can try it over again.

The gentleman in the rear has another question. Yes, sir?

He asks if this isn’t the exact opposite of what we set out to accomplish? He says that he can see how a string of characters can be broken apart but is wondering how sets of strings of characters can be brought together into one common formatted variable?

Patience, my friend. We must crawl before we walk and roll before we crawl. ENTER the following program:

FIELD PROGRAM

10 DIM A$(5,32)
20 FOR N = 1 TO 5
30 SCROLL
40 PRINT "NAME?",
50 INPUT B$
60 LET A$(N, TO 12) = B$
70 PRINT B$
80 SCROLL
90 PRINT "ADDRESS?",
100 INPUT B$
110 LET A$(N,13 TO 24) = B$
120 PRINT B$
130 SCROLL
140 PRINT "PHONE NO.?",
150 INPUT B$
160 LET A$(N,25 TO 32) = B$
170 PRINT B$
180 SCROLL
190 SCROLL
200 NEXT N
210 SCROLL
220 SCROLL
230 PRINT "PRESS ENTER TO
SEE VARIABLES :::"
240 SCROLL
250 SCROLL
260 SCROLL
270 INPUT B$
280 FOR N = 1 TO 5
290 SCROLL
300 PRINT A$(N)
310 SCROLL
320 PRINT "12345678901234567890
123456789012"
330 SCROLL
340 SCROLL
350 NEXT N
360 SCROLL
370 SCROLL
380 PRINT "PRESS ENTER TO SEE
SEGMENTS :::"
390 INPUT B$
400 SCROLL
410 SCROLL
420 FOR N = 1 TO 5
430 PRINT "NAME",A$(N, TO 12)
440 SCROLL
450 PRINT "ADDRESS",A$(N, 13
TO 24)
460 SCROLL
470 PRINT "PHONE NO.",A$(N,
25 TO 32)
480 SCROLL
490 SCROLL
500 NEXT N

RUN this program and enter five names, addresses and phone numbers. You are limited to 12 characters for the name, 12 characters for the address and 8 characters for the phone numbers. Purposely enter more than this limit for some of the items. You will notice later that the computer will automatically truncate them to the limited amount of characters.

After the fifth set is entered, you will be given the instruction:

PRESS ENTER TO
SEE VARIABLES :::

Press ENTER and the variables will be displayed with the 32 numbers below them for identifying the character positions. The numbers start with 1 and go to 9 which is followed by 0, 1, 2 etc. The first character of the 2 digit numbers is omitted.

This entire program was written with only two string variables and yet we have three separate pieces of information under program control. Actually, these three pieces of information are in only one string variable.

CREATING DATA FIELDS

I previously mentioned Random Access data manipulation methods. These methods incorporate a technique to create FIELDS within a string variable. If you look at your screen, you will see the data you previously entered now formatted into fields within single string variables withing an array. The array is the 5 total string variables. The fields of each variable are as follows:

  • character positions 1 through 12 represent the NAME information.
  • character positions 13 through 24 represent the ADDRESS information.
  • character positions 25 through 32 represent the PHONE NUMBER information.

FIELDING RULES

The following rules apply when data is entered into these fields:

  • the first character of the FIELD item or element corresponds with the first FIELD position assigned to that element in the dimensionalized string variable (line 10) by the formatting program routine assigned to that task (lines 60, 110 and 160).
  • all characters following the first character of the FIELD element will take the next available positions in the corresponding dimentionalized string variable.
  • all positions of the dimensionalized string variable are null spaces (blank) until they are replaced with characters (line 10 makes all the positions blank and lines 60, 110 and 160 replaces these blank spaces with the data derived from lines 50, 100 and 150).
  • if the amount of characters of the data to be inserted into a FIELD position exceeds the amount of space allowed, the data will be truncated (chopped off) from its right side to accommodate the space allowed.
  • if the amount of characters of the data to be inserted into a FIELD position is LESS than the amount of space allowed, blank spaces will follow the data to take up the remaining space.

Study these variables and the positions of the characters in them. You see the message at the bottom of the screen:

PRESS ENTER TO SEE SEGMENTS :::

Do so and you will see all of the elements are broken out of the string variables. We now have demonstrated one method of putting a bunch of elements into a single string variable and breaking them out again.

But in the immortal words of that famous and world reknowned philosopher, Alfred E. Neuman, “You Ain’t Seen Nuttin’ Yet”.

SORTING BY FIELDS

What you are about to see is an extremely powerful string manipulating technique. Add the following routine to the last program. CAUTION – DO NOT RUN THE PROGRAM AFTER THIS ROUTINE IS ENTERED!

510 SCROLL
520 SCROLL
530 PRINT "PRESS ENTER
TO CONTINUE :::"
540 INPUT B$
600 FAST
610 CLS
620 SLOW
630 PRINT "WHICH FIELD DO YOU
WANT TO SORT","(NAME -1/
ADDRESS -2/ PHONE -3)"
640 INPUT A
650 IF CODE STR$ A<29 OR CODE
STR$ A>31 THEN GOTO 640
660 FAST
670 GOTO 600 + (A*100)
700 LET Y = 1
710 LET Z = 12
720 GOTO 1000
800 LET Y = 13
810 LET Z = 24
820 GOTO 1000
900 LET Y = 25
910 LET Z = 32
1000 CLS
1010 LET N = 5
1020 LET N = INT N/2
1030 IF N = 0 THEN GOTO 1200
1040 LET J = 1
1050 LET K = 5 - N
1060 LET I = J
1070 LET L = I + N
1080 IF A$(I,Y TO Z)<=A$(L,
Y TO Z) THEN GOTO 1150
1090 LET B$ = A$(I)
1100 LET A$(I) = A$(L)
1110 LET A$(L) = B$
1120 LET I = I - N
1130 IF I<1 THEN GOTO 1150
1140 GOTO 1070
1150 LET J = J + 1
1160 IF J>K THEN GOTO 1020
1170 GOTO 1060
1200 SLOW
1210 GOTO 210

You will notice that the above routine is almost identical to the SHELL SORT on page 11 of the OCT/82 issue. Lines 1010 and 1050 have changed the variable (B) to (5). This variable and number correspond with the total amount of strings to be sorted.

Now ENTER (GOTO 210). Press ENTER and the data you entered previously should be displayed. Press ENTER again and the FIELD ELEMENTS will be displayed. Now press ENTER and choose one of the fields you want sorted. The following 2 displays will show the data being sorted by the command you gave. If you chose NAME – 1, the names will be in alphabetical order. If address or phone number were chosen, the string variables would have been sorted by those fields.

I might mention that I am not aware of any other computer that allows this format and speed to accomplish this task and a program could be written to allow as many ELEMENT FIELDS per string variable as the computer’s memory would allow.

This could he a very valuble technique in writing an inventory program and is used in the BILLPAYER MACHINE of the HOME BUDGET package marketed by SYNCHRO-SETTE.

This, by no means, is all that can be done by manipulating string variables. The techniques are only limited by the programmer’s imagination.

For your homework, try to write a program that has at least 12 elements per variable, 50 total variables and allows a sort for any element category.

HEY, YOU IN THE BACK! I TOLD YOU TO PUT THAT YO-YO AWAY!

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.