In Fortran characters may be treated individually or as contiguous strings. Strings have a specific length and individual characters within the string are referred to by position, the left most character at position 1, etc. As with numeric data the programmer may specify literal constants of intrinsic type character as described below.
WRITE (6,*) `Please type a value for the radius of a circle'
READ (5,*) radius
area = pi*radius*radius
WRITE (6,*) `The area of a circle of radius `, radius, &
` is `, area
The characters which appear between pairs of apostrophes are character constants and will appear on screen as
Please type a value for the radius of a circle
12.0
The area of a circle of radius 12.0 is 452.38925
The double quote character may also be used to define character literals. If a string of characters is to contain one of the delimiting characters then the other may be used. However if the string is to contain both delimiting characters or a programmer wishes to always define strings using the same character then the delimiter may appear in a string as two adjacent apostrophes or double quotes. These are then treated as a single character.
"This string contains an apostrophe `."
`This string contains a double quote " .`
"This string contains an apostrophe ` and a double quote ""."
This would appear in output as
This string contains an apostrophe `.
This string contains a double quote ".
This string contains an apostrophe ` and a double quote ".
CHARACTER :: yesorno, sex
A value may be assigned to a character variable in the form of a literal constant thus
yesorno = `N'
sex = `F'
However character variables are more frequently used to store multiple characters known as strings. For example to store a person's name the following declarations and assignments may be made (note the use of the keyword len)
CHARACTER (LEN=12) :: surname, firstname
CHARACTER (LEN=6) :: initials, title
title = `Prof.`
initials = `fjs`
firstname = `Fred`
surname = `Bloggs`
Notice that all the strings were defined as being long enough to contain the literal constants assigned. Variables which have unused characters are space filled at the end. If the variable is not large enough to contain the characters assigned to it then the leftmost are used and the excess truncated, for example
title = `Professor`
would be equivalent to
title = `Profes`
The general form of a character declaration is:
CHARACTER [(len= )] [,attributes] :: name
CHARACTER (len=24) :: name
CHARACTER (len=6) :: surname
surname = `Bloggs'
name = `Prof `//` Fred `//surname
As with character literals if the expression using the // operator exceeds the length of the variable the rightmost characters are truncated and if too few characters are specified the rightmost characters are filled with spaces.
CHARACTER (LEN=7) :: lang
lang = `Fortran'
WRITE (6,*) lang(1:1), lang(2:2), lang(3:4), lang(5:7)
would produce the following output
Fortran
The substring is specified as (start-position : end-position). If the value for start-position is omitted 1 is assumed and if the value for end-position is omitted the value of the maximum length of the string is assumed thus, lang(:3) is equivalent to lang(1:3) and lang(5:) is equivalent to lang(5:7).
The start-position and end-position values must be integers or expressions yielding integer values. The start-position must always be greater than or equal to 1 and the end-position less than or equal to the string length. If the start-position is greater than the maximum length or the end-position then a string of zero length is the result.
The conversion between characters and integers is based on the fact that the available characters form a sequence and the integer values represent the position within a sequence. As there are several possible character sequences and these are machine dependent the precise integer values are not discussed here. However, it is possible to state that regardless of the actual sequence the following are possible:
INTEGER :: i
CHARACTER :: ch
...
i=ICHAR(CHAR(i))
ch=CHAR(ICHAR(ch)
Below is an example of how intrinsic functions may be used:
CHARACTER(len=12) :: surname, firstname
INTEGER :: length, pos
...
length = LEN(surname) !len=12
firstname = `Walter`
pos = INDEX(`al`, firstname) !pos=2
firstname = `Fred`
pos = INDEX(`al`, firstname) !pos=0
length = LEN(TRIM(firstname)) !len=4
CHARACTER(len=5) :: vowels=`aeiou`
what are the substrings specified below?
(a) vowels(1:1)
(b) vowels(:2)
(c) vowels(4:)
(d) vowels(2:4)
CHARACTER(len=27) :: title=`An Introduction to Fortran.'
define substrings which would specify the character literals below?
(a) to
(b) Intro
(c) Fortran.
(a) find the location of the string duct
(b) find the length of the string
(c) extract and concatenate substrings to produce the string Fortran, An Introduction to.
Mr. Joseph Bloggs,
12, Oil Drum Lane,
Anytown,
United Kingbom
JF Bloggs, 12 Oil Drum Lane, Anytown