The Queen's University of Belfast
Parallel Computer Centre

[Next] [Previous] [Top]

3 Character Processing


3.1 Character Type

In the previous chapter the intrinsic numeric types real and integer were introduced, a third intrinsic type character is presented in this section. This type is used when the data which is being manipulated is in the form of characters and words rather than numbers. Character handling is very important in numeric applications as the input or output of undocumented numbers is not very user friendly.

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.

3.2 Character Constants

The example below is taken from a program which calculates the area of a circle, the program reads in a value for the radius and writes out the area of the circle. Without prompts the user`s view of such a program is very bleak, that is there is no indication of what the input is for or when it should be supplied nor is there an explanation of the output. By including some character constants (or literals) in the output the user's view of the program can be greatly enhanced, for example

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 ".

3.3 Character Variables

The declaration of character variables is similar to that for real and integer variables. the following statement declares two character variables each of which can contain a single character

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

3.4 Character manipulation

3.4.1 Concatenation

The arithmetic operators such as + - should not be used with character variables. The only operator for character variables is the concatenation operator //. This may be used to join two strings as follows

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.

3.4.2 Substrings

As the name suggests substrings are sections of larger character strings. The characters in a string may be referred to by position within the string starting from character 1 the leftmost character.

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.

3.4.3 Intrinsic Functions

Functions will be dealt with in more depth later in the course, however it is convenient to introduce some functions at this early stage. An intrinsic function performs an action which is defined by the language standard and the functions tabulated below relate to character strings. These intrinsic functions perform a number of commonly required character manipulations which programmers would otherwise have to write themselves.

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

3.5 Exercises

  1. Given the following variable declaration and initialization:

    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)

  2. Given the following variable declaration and initialization:

    CHARACTER(len=27) :: title=`An Introduction to Fortran.'

    define substrings which would specify the character literals below?

    (a) to

    (b) Intro

    (c) Fortran.

  3. Using the variable title defined above write expressions, using the intrinsic functions, which would

    (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.

  4. Write a program which would test the results of the expressions defined in the previous exercise.

  5. Design a derived data type which contains the following details relating to yourself: surname, forename, intials, title and address. The address should be a further derived type containing house number, street, town/city and country.

  6. Write a Fortran program which will request input corresponding to your name and address as defined in the text and which will output your name and address in two forms as follows:

    Mr. Joseph Bloggs,

    12, Oil Drum Lane,

    Anytown,

    United Kingbom

    JF Bloggs, 12 Oil Drum Lane, Anytown


[Next] [Previous] [Top]
All documents are the responsibility of, and copyright, © their authors and do not represent the views of The Parallel Computer Centre, nor of The Queen's University of Belfast.
Maintained by Alan Rea, email A.Rea@qub.ac.uk
Generated with CERN WebMaker