• Aucun résultat trouvé

character in a string. That means that like LEN, the argument LJ

Dans le document Publishing is (Page 178-182)

has to be a string, and the value returned is always a number.

PRINT ASCC'ALPHABET") i j

The above example printed the number 65. The ASC function looks at only the first character of the string. If the string

168

LJ

LJ

n

String Variables and Functions

n n n n

contains other characters, they are ignored. The range of values that can be returned by ASC is from 0 to 255.

One precaution is necessary when using this function.

The null string contains no characters, so there is no way to determine an ASCII value. Using a null string as the argument for the ASC function produces the ILLEGAL QUANTITY

error.

10 GET A$ : IF A$="M GOTO 10 :rem 239

20 PRINT ASC(A$) : GOTO 10 :rem 80

Try typing the various keys on the keyboard. You will see that each key which prints a character has an ASCII value. Next, try pressing the special function keys. These keys have ASCII values, too. Now you know how a program can determine if the special function keys have been pressed. This also applies to other special keys like the screen clear and cursor move ment keys.

The last function is a little different from the first two.

Unlike LEN and ASC, which require strings as arguments, CHR$ needs a number, specifically an ASCII number. Also, CHR$ returns a string, not a number. This is indicated by the

$ at the end of the function name. The CHR$ function takes an ASCII value and returns the corresponding character. Thus, CHR$(65) can be used just like the character string A. It could even be concatenated to another string. One use for the CHR$

function is to generate the double quote character, which is otherwise impossible to put in a character string. The ASCII code for the double quote is 34.

PRINT "CHRIS SAID, //;CHR$(34);//WOW!//;CHR$(34) The CHR$ function can be used to print any character.

10 K=32 :rem 78

20 PRINT CHR$(K);M "; :rem 70

|—i 30 K=K+1 : IF K<147 GOTO 20 :rem 25 CHR$ is often an easier way to print special characters, which are difficult to put between quote marks. Notice that the

[""] READY prompt and cursor are in black. This was done by

character 144, one of the color-changing control codes. The program stopped just short of character 147, which is the code

["""I to clear the screen. By using all of the color-changing character

codes, a colorful demonstration program can be written.

H

u

String Variables and Functions

10 REM RAINBOW :rem 87 LJ

20 PRINT CHR$(147) : REM CLEAR SCREEN :rem 34 30 READ C : IP C=-999 THEN END :rem 233

40 PRINT CHR$(C),"{4 SPACES}COMMODORE 64M :rem 5 j

50 GOTO 30 :rem 1 L-J

60 DATA 5,28,30,31,129,144,149,150,151 :rem 70

70 DATA 152,153,154,155,156,158,159,-999 :rem 204 r , One line appears to be missing because it is in the same color

as the background.

Summary

• Several functions are available for use with character strings.

The three functions introduced in this section are LEN, ASC, and CHR$.

• The LEN and ASC functions require a string for an argu ment and return a number. This is reversed for CHR$, which needs a number for an argument and returns a string. The presence of the $ at the end of the function name indicates that the function returns a character string.

• The LEN function is used to count the number of characters in a string. The range of this function is 0 to 255, with 0 being the length of the null string.

• The ASC function returns the ASCII code for the first character in a string. The string must not be the null string, or the ILLEGAL QUANTITY error will be printed. ASC is often used to check input characters. The special function keys have ASCII codes which can be detected by this function.

• The CHR$ function takes an ASCII code and returns a string one character long, the character of the given ASCII code. A common use for this function is to print the double quote character. It also simplifies the printing of special control

codes, such as those for screen clearing and color changing. i . Type Conversion Functions

Thus far, every attempt to mix numbers and character strings !i in the same operation has been greeted by the TYPE MIS

MATCH error. Nevertheless, there will be occasions when a

string consisting of digit characters must be used like a num- M ber, or when a number must be treated as if it were a string.

To do this, you must convert the value in question from one 170

U

LJ

n n n n

String Variables and Functions

type to the other. There are two directions for conversion, so there are two functions that perform the conversions. The VAL function is used to convert a character string to the type number, and the function STR$ is used to convert a number to the type character string.

The VAL function returns the numeric value of a charac ter string. Given a string, VAL first skips past any leading spaces. Next, VAL looks at the remaining characters in the string, stopping only when it comes to the end of the string, or when a nonnumeric character is found. The numeric value of the string number is then returned by VAL.

10 N$=M{3 SPACES}37 DEGREES CELSIUS" :rem 213 20 PRINT "THE TEMPERATURE IS"; 100-VAL(N$);

:rem 216 30 PRINT "DEGREES BELOW THE BOILING POINT" :rem 27 The computer printed a 63 on the screen. The characters which have to do with numbers are the plus and minus signs, the period (decimal point), and of course the ten digits.

VAL returns 0 for the null string or strings which do not start with any characters used with numbers.

PRINT VAL("CHRIS")

The STR$ function takes any number and returns the charac ter string representation of that number. Unlike CHR$, which returns only a single character, STR$ may return several characters.

10 INPUT "WHAT IS YOUR FAVORITE NUMBER";N :rem 6 20 PRINT "THE NUMBER" N "HAS" LEN(STR$(N))-l "DIGI

T(S)" :rem 238

The character string may contain any of the characters related to numbers, which were listed above. The characters in a string produced by STR$ are always identical to the characters that would appear on the screen if the number were printed.

The first character is reserved for the sign of the number, which is why the number 1 was subtracted from the length in the example. The sign character will be a minus sign if the number is negative; otherwise, it will be a space.

n n n n

171

H

u String Variables and Functions

u

Summary LJ

• The VAL and STR$ functions are used to convert a value

from one type to the other, to avoid the TYPE MISMATCH j -j

error. lj

• The VAL function takes a string, interprets the characters

which are related to numbers, and returns a value. j ,

• VAL ignores any leading spaces. •—'

• The interpretation uses all characters in the string, starting at the beginning, until the end of the string is reached, or a character is found which is not related to numbers.

• The only characters which are related to numbers are the ten digits, the plus and minus signs, and the period.

• The null string and strings which start with a nonnumeric character cause VAL to.return a value of 0.

• The STR$ function takes a number and produces its charac ter representation. This representation includes all the characters that would be used if the number were printed.

If the number is positive, the string will start with a space.

Substring Functions

Concatenation can be used to put strings together, but there is no operation to take strings apart. Sometimes, however, it is handy to look at selected characters in a string—just a part of the string, not the whole string. So Commodore 64 BASIC has three functions designed to work with parts of character strings. A sequence of characters contained in a string is called a substring, which explains the name substring functions.

The substring functions, LEFT$, RIGHTS, and MID$, are inconsistent with the other functions we have used so far.

First, not all of the names of these functions are exactly three characters long. Also, these functions have more than one argument. Further, the arguments used in these functions are

Dans le document Publishing is (Page 178-182)