



Character Intrinsic Functions These two functions perform integer to character conversion and vice-versa using
the internal code of the machine. Although most computers now use the ASCII
character code, it is by no means universal, so these functions can only be used in a
very limited way in portable software.
CHAR(I) returns the character at position I in the code table. For example, on a
machine using ASCII code, CHAR(74) = 'J', since "J" is the character number 74 in
the ASCII code table.
ICHAR(STRING) returns the integer position in the code table of the first
character of the argument STRING. For example, on a machine using ASCII
code,
ICHAR('JOHN')
74
ICHAR('john')
106
INDEX is a search function; it takes two character arguments and returns an
integer result. INDEX(S1, S2) searches for the character-string S2 in another string
S1, which is usually longer. If S2 is present in S1 the function returns the character
position at which it finds starts. If there is no match (or S1 is shorter than S2) then it
returns the value zero. For example:
CHARACTER*20 SPELL SPELL = 'ABRACADABRA' K = INDEX(SPELL, 'RA') |
K will be set to 3 because this is the position of the first occurrence of the
string 'RA'. To find the second occurrence it is necessary to restart the search at the
next character in the main string, for example:
L = INDEX(SPELL(K+1:), 'RA')
This will return the value 7 because the first occurrence of 'RA' in the substring
'ACADABRA' is at position 7. To find its position in the parent string the offset, K,
must be added, making 10.
The INDEX function is often useful when manipulating character information.
Suppose, for example, we have an string NAME containing the a person's surname
and initials, e.g.
Mozart,W.A
The name can be reformatted to put the initials before the surname and omit the
comma like this:
CHARACTER NAME*25, PERSON*25 *... KCOMMA = INDEX(NAME, ',') KSPACE = INDEX(NAME, ' ') PERSON = NAME(KCOMMA+1:KSPACE-1) // NAME(1:KCOMMA-1) |
'W.A.Mozart' (with blanks appended to the
length of 25). Note that a separate variable, PERSON, was necessary because of the
rule about overlapping strings in assignments.
The LEN function takes a character argument and returns its length as an integer.
The argument may be a local character variable or array element but this will just
return a constant. LEN is more useful in procedures where character dummy
arguments (and character function names) may have their length passed over from
the calling unit, so that the length may be different on each procedure call. The
length returned by LEN is that declared for the item. Sometimes it is more useful to
find the length excluding trailing blanks. The next function does just that, using LEN
in the process.
INTEGER FUNCTION LENGTH(STRING)
*Returns length of string ignoring trailing blanks
CHARACTER*(*) STRING
DO 15, I = LEN(STRING), 1, -1
IF(STRING(I:I) .NE. ' ') GO TO 20
15 CONTINUE
20 LENGTH = I
END
|