BBC BASIC 2.31 for the Tatung Einstein, showing DRIFT, the text adventure the course builds, in the middle of a game
← Back to Courses
module
18

Taking A Sentence Apart

Introduction

In DRIFT the player types what they want to do - GET PAD, GO NORTH, USE CELL - and the program has to make sense of it. The first step is to take the sentence apart: which word is the action, and which is the thing. BBC BASIC has a small set of tools for cutting strings up, and this section is about them.

Finding Something In A String

INSTR looks for one string inside another and says where it is:

PRINT INSTR("GET PAD"," ")

prints 4 - the space is the fourth character. Counting starts at 1. If it is not there at all, INSTR gives 0, which makes it a good test: IF INSTR(C$,"PAD") THEN ... is true whenever PAD appears anywhere in C$.

A third number says where to start looking: INSTR(C$," ",5) finds the first space from position 5 on.

No space between INSTR and its bracket. INSTR ("GET PAD"," ") is not INSTR at all to BBC BASIC; the same goes for the three below.

Cutting A String

givesfrom "ENGINEERING"
LEFT$(A$,n)the first n charactersLEFT$(A$,3) is ENG
RIGHT$(A$,n)the last nRIGHT$(A$,4) is RING
MID$(A$,p,n)n characters from position pMID$(A$,4,3) is INE
MID$(A$,p)everything from position pMID$(A$,6) is EERING

Ask for more than there is and you get what there is; start past the end and you get an empty string. None of them is ever an error.

Verb And Noun

Put them together and a sentence comes apart at its first space:

S=INSTR(C$," ")
V$=LEFT$(C$,S-1)
N$=MID$(C$,S+1)

Everything before the space is the verb; everything after it is the noun. If there is no space - just LOOK - S is 0, and the whole thing is the verb.

Capitals

"GET" and "get" are different strings to BBC BASIC: comparisons are exact. The player may type either, so the program turns everything into capitals before looking at it.

Every character has a code number, which ASC gives and CHR$ turns back: ASC("A") is 65 and CHR$(65) is A. The small letters a to z are the codes 97 to 122, and each is exactly 32 more than its capital. So: go through the string a character at a time; if its code is between 97 and 122, take 32 off; put the character back. That is FNCAPS$ below.

The Code

10 INPUT LINE "WHAT NOW",C$
20 C$=FNCAPS$(C$)
30 S=INSTR(C$," ")
40 IF S=0 THEN V$=C$:N$="" ELSE V$=LEFT$(C$,S-1):N$=MID$(C$,S+1)
50 PRINT "VERB: ";V$
60 PRINT "NOUN: ";N$
70 END
100 DEF FNCAPS$(A$)
110 LOCAL I,C,B$
120 FOR I=1 TO LEN(A$)
130 C=ASC(MID$(A$,I,1))
140 IF C>=97 AND C<=122 THEN C=C-32
150 B$=B$+CHR$(C)
160 NEXT I
170 =B$

Starting from

A fresh boot, or NEW.

What you should see

Answering GET PAD:

>RUN
WHAT NOW? GET PAD
VERB: GET
NOUN: PAD
>

INPUT LINE is used so that a comma in the reply does not cut it short.

A command split into verb and noun

Change One Thing

  • Run it and answer just LOOK. What are the verb and the noun?
  • Answer with three spaces before GET PAD. What goes wrong, and why?
  • Press CAPS LOCK, run it (typing RUN with SHIFT), and answer get pad in small letters. What does FNCAPS$ do with it? Press CAPS LOCK again after.

Exercises

18.1 Ask for a word and print its first three letters.

18.2 Ask for a sentence and count the spaces in it.

18.3 Write FNTRIM$(A$), which gives A$ with any spaces at the front removed - the cure for the second change above.

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
No such variable at INSTR, LEFT$, MID$ or RIGHT$A space before the bracket.
The verb comes out emptyThe reply started with a space, so the first space is at position 1. Trim the front first.
A word typed in small letters is not recognisedComparisons are exact. Turn it into capitals first.

Summary

INSTR finds a string in another, LEFT$, RIGHT$ and MID$ cut pieces out, and ASC and CHR$ turn characters into codes and back. Split at the first space for a verb and a noun; make capitals first so the player's CAPS LOCK does not matter.

Next

S19, Colour And Where Text Goes - putting words anywhere on the screen, in colour.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.