INTERMEDIATE SECTION C FILE INTER.C STRING MANIPULATION ******************* Let us recap on PRIMER by defining a 'string'. A string is a sequence of zero to a maximum of 255 characters enclosed in double quotes or starting with a double quote, terminated with a carriage return [RETURN] The characters can be letters, figures, spaces and most symbols ( some symbols e.g (") are not accepted and others may be 'dodgy' if you need to print to paper( the printer will not print all symbols) Typical strings are :- a) " The boy stood on the burning deck " (simple text string) b) "" (an important special case with no characters- a null string) c) "345" "0.385" " 0095 "(numeric strings print exactly as written) d) " The room was 12 x 18 metres in area " Within the constraints indicated above any sequence of characters within double quotes will print out exactly as typed. also strings can be stored in string variables e.g name$ = "John Williams" On the command PRINT name$, John Williams will be displayed (without the quote marks. All this has been covered in PRIMER, but in this section I want to introduce the manipulation of strings. This is a wide and very important subject. By enclosing the string in quotes you identify it as a string to the computer, so that it knows how to deal with it. But although the string may appear to be isolated within the quotes it can, in fact, be accessed in many ways. There follows a description of the functions that allow you to find out information about any given string. Remember that these functions work equally well when, as is often the case, the string is represented by a string variable.A simple variable such as a$ can represent a string of up to 255 characters. Note that quotation marks are needed when dealing with an actual string, but not when dealing with a string variable.This rule is common to all string manipulations. LEN(" String ") gives the number of characters in the string, in *************** this case 8 ( don't forget the spaces at each end) LEN( a$ ) would give the number of characters in the string ********* which a$ represents LEFT$("Any string",5) would return the five leftmost characters ********************* 'Any s' (once again, note the space is counted) LEFT$(a$,7) 'Nuff said I hope ? *********** RIGHT$ is used in the same way but returns characters from the ****** righthand side. MID$("Any string",3,4) returns four characters, starting at the ********************* third i.e 'y st' MID$(a$,5,3) Again the string variable is treated the same but no ************ quotation marks required. INSTR("Any string", a$) searches the string to see if it contains *********************** the string which a$ represents. For example if a$ = "in", then the number 8 would be returned, showing that the string "in" starts at the eighth character from the left. If a$ = "x", INSTR would draw a blank and return 0 (zero). INSTR( 5,"Any string",a$) This is as before except that INSTR ************************* ignores the first five characters. UPPER$(a$) ensures that all letters in the string represented by a$ ********** will be in upper case. UPPER$ only works with variables, not directly with strings. LOWER$(a$) ensures that all letters in the string represented by a$ ********** will be in lower case . LOWER$ only works with variables, not directly with strings. **** NUMERICAL STRINGS*** Numbers can be held in strings and stored in string variables although they cannot be used mathematically in this form. All the above functions work with number strings except that UPPER$ and LOWER$ have no apparent effect as they ignore numbers and only work on letters. Still, if you're working with mixed strings it's good to know that all these commands can be used without giving rise to an error message if a string composed entirely of numbers should be met. The following commands are mainly for use with numeric strings and an attempt to use them with another type may not work . VAL(a$) converts the string represented by the variable a$ to a ******* number which can be used mathematically. In other words it 'releases' the number from the constraining quote marks. If a mixed string is encountered,starting with a number,say " 19th March " VAL would return 19. HEX$(b) converts b into hexadecimal format, where b is a numeric ******* variable representing a decimal system number. HEX$ deals only with integers(whole numbers) and if the number has a decimal part will round to the nearest integer.It also works directly with numbers. Examples HEX$(45) = 2E HEX$(45.6)=2D OCT$(b) works in the same way as HEX$ except that it converts ******* decimal numbers into octal format(base 8) DEC$ doesn't work at all in the same way as OCT$ and HEX$, but **** converts decimal numbers into a pre-specified format, like the PRINT USING command, but with fewer options + - $ # , . ^. The first step is to form a string of relevant characters e.g b$="###.##".Then DEC$(142.3,b$) will return 142.300, or DEC(23.86574,b$) will return 23.866. More information on possible formats is given in INTER section (b - Mathematics. **** STORING NUMBERS AS STRINGS **** When numbers are stored in the normal way as a string or a variable, each digit takes up a unit of memory called a byte.Thus a number with eight digits would use up 8 bytes and so on. This does not usually present any difficulty unless a lot of numbers have to be stored, as in a random access file or database. Although this course does not deal with these files, it is interesting and appropriate to discuss how numbers can be filed in a memory-saving way. MKI$(Integer) converts an integer into a 2-byte string CVI(variable%) converts the 2-byte string back to integer form MKS$(single length value) converts the value to a 4-byte string CVS (variable!) converts the 4-byte string back to single length MKD$(double length value) converts the value to an 8-byte string CVD(variable#) converts the 8-byte string back to double length. As stated, these functions are mainly for use with record files where memory has to be allocated in advance for all data entered. If, for example you have stock records where quantities can vary between say 0 and 100,000 it is useful to be able to store any integer number in the same amount of memory i.e 2 bytes. The same principle applies where mixed numbers are stored in engineering or scientific records as single or double precision numbers. End of file INTER.C ed in engineering or scientific records as single or double precision numbers. End of file INTER