Characters in Fortran


In Fortran, the alphanumeric (character) variables must have a specific length when declared. For example, let's see the following declarations:





character(len=10) :: name, surname
character :: firstLetter
character(16) :: nickname

The above declarations define two 10-character variables (name and surname), one 1-character variable named firstLetter and a 16-character variable named nickname.
The older FORTRAN 77 syntax for the 'nickname' variable would be:

character*16 nickname

This form of type declaration statement has been declared OBSOLESCENT in Fortran 95 and should never be used in any new programs.

Character literals
Character literals are either between single or double quotes. If a character string is between simple quotes and includes an apostrophe then the apostrophe may be represented by two single quotes. The two following examples are equivalent:

phrase = 'Man''s best try'
phrase = "Man's best try"

Depending on the case, single or double quotes may be more convenient:

phrase = '"Who is here?"'

To concatenate character strings in Fortran we can use the // operator. For example all three lines below are equivalent:

"AB" // 'CDE'
'ABC' // "DE"
'ABCDE'

To return a substring, we can use the common array subset notation:

name = "Keftes"
firstTwo = name(1:2)
!we use the same notation even if we want a single character
secondChar=name(2:2)

Passing characters to functions
A character variable in an argument list may of course be declared as below:

subroutine printchar(chr,n)
integer, intent(in) :: n
character(len=n) :: chr

To declare character arguments in procedures, Fortran allows a special more practical form of declaration:

character(len=*) :: var

Its length is not explicitly known at compilation time but can be retrieved by calling the LEN function. The good thing is that characters of any length may be accepted by using this syntax.
The following declarations of character arguments are practically the same:

subroutine printchar(chr,chr2,chr3)
character(len=*) :: chr !recommended
character*(*)::chr2
character :: chr3*(*)
print *,chr,chr2,chr3
...

We can also create an automatic character string for use inside a procedure:

subroutine sample ( string )
!the string must be allocated externally
character(len=*) :: string
!the temp is allocated inside the procedure
character(len=len(string)) :: temp

The READ function
We can use the read internal function to convert from string literals to numeric values:

character(20)::string="3.141592"
real::value
!value gets the value by reading the string
read(string,'(F8.6)') value

Functions used with characters
Check the following functions that can be used with characters:

CHAR(ival)
Returns the character from a number in the ASCII set.

ICHAR(ival)

Returns the integer number in the ASCII sequence from a character.

INDEX(str1,str2,back)

Returns the first location of str2 inside the str1 character. Returns 0 if there is no match. If back is specified and set to .true. then the search starts from the end.

SCAN(str1,set,back)

Returns the first location of any of the 'set' characters inside the str1 character string. Returns 0 if there is no match.

VERIFY(str1,set,back)
Returns the first location of any of the characters NOT in the 'set', inside the str1 character string. Returns 0 if all characters in 'set' exist in the str1 character string.

TRIM(str)

Returns the input string without trailing spaces.

LEN(str)

Returns the length of the string.

LEN_TRIM(str)
Returns the length of the string excluding any trailing spaces.

LLT(str1,str2)

Returns .true. if str1<str2 (case insensitive).

LLE(str1,str2)

Returns .true. if str1≤str2 (case insensitive).

LGT(str1,str2)

Returns .true. if str1>str2 (case insensitive).

LGE(str1,str2)

Returns .true. if str1≥str2 (case insensitive).

Comments

Zaak said…
In Fortran, the alphanumeric (character) variables must have a specific length when declared.

This is no longer true with the introduction of allocatable scalars in Fortran 2003.

Popular posts from this blog

Write Unicode text using VBA

Calling Fortran intrinsic functions from Visual Basic

Dictionary class extensions (CopyTo, Sort) (C#)