Character
Type
Non-numerical Data
Character
Variables
• not
all data in ‘real’ life is numerical
• examples
– names of
persons, objects, places etc
– textual
material
– pictures
– sounds
• majority
of today’s applications are not numerical
• textual
data is the most common
• a
sequence or string of allowed
‘characters’
• a
fixed set of characters is allowed
– letters a …
z and A … Z
– digits 0 … 9
– punctuation
marks . , ? “ ; etc
– some special
characters like blank, +, %
Declaration
of Characters
• Fortran
allows declaration of variables of type character
character(len = 20) :: name
• declares
name to be a character variable whose value can be any string of characters
containing exactly 20 characters
• possible
values for name
– “ram ” ( note 17 blank symbols)
– “sachin
r. tendulkar”
Representation
of Characters
• typically,
8 bits are used for representing a single character
• a
standard code called ASCII is used
• ASCII
assigns a distinct 8 bit integer to
each allowed character
• a
32-bit location contains 4 ASCII characters
• most
computers follow the ASCII code but Fortran does not assume it
• “a”
is coded by 97 while “A” is 65
Character
Constants
• character
constants are specified by enclosing the string of characters in quotes “ … ”
– “cs101”,
“FORTRAN programming”
• if
“ is in the string, it should be repeated
– “he said
““hello””” is the string
– he said
“hello”
• length
of character constants is assumed
character(len=*),parameter :: name=“ram”
Assignment
Statement
• assignment
statement used for assigning values to character variables
• only
character variables and/or constants can appear on the right hand side
• lengths
of variables or constants may be different
• values
are either truncated or padded with blanks ,if required, before assignment
Examples
of Assignment
character(len=5) :: name
character(len=10) ::
full_name
name = “sachin”
! name has value “sachi”, n is truncated
full_name = “tendulkar”
! full_name is “tendulkar “, blank is added
full_name = name
!full_name is now “sachi “
Operations
on Characters
• only
one operation concatenation, denoted //
• string1//string2
denotes the string obtained by appending string2 at then end of string1
character(len=10) :: name, surname
character(len=20) :: full_name
name = “sachin” ; surname =
“tendulkar”
full_name = name//surname
! full_name is the string “sachin tendulkar “
Substrings
• if
c is a character variable, c(i : j) is the string containing characters of c
from positions i to j
• c(i:j)
is called a substring of c
– same storage
used for c and c(i : j)
– values may
be assigned to c(i : j)
– if j < i,
c(i : j) is a null string of length 0
– if i is
missing it is taken as 1
– if j is
missing it is the length of string c
Examples
of Substrings
character(len = 10) :: name
name = “Ajit Diwan”
name = name(6:)//“ ”//name(:4)
! name will now be “Diwan Ajit”
name(3:5) = “ghe”
!name will now be “Dighe Ajit”
name(8:) = “nil”
!name will now be “Dighe Anil”
Comparison
of Characters
• variables
of character type may be compared using the same relational operators
• blanks
are appended to the shorter string to make strings being compared of same
length
• single
characters are compared based on their integer codes
• results
may differ on different machines
• strings
are compared by comparing leftmost characters in which they differ
Examples
of Comparison
character(len=10) :: name, surname
name = “sachin”
surname = “tendulkar”
• name
< surname is true ( “s” < “t” in
ASCII)
• name
< name//surname is also true ( blanks are < all allowed characters
• name
== “sachin” is also true
• name
== “ sachin” is false
Intrinsic
Functions
• Fortran
provides several built-in intrinsic functions for character manipulation
• examples
– assume c is
a character variable
• len(c)
– gives the
length of c (an integer)
• len_trim(c)
– gives the
length of c after removing trailing blanks
• trim(c)
– gives the
string obtained by removing trailing blanks from c
• iachar(c)
– c must be a
character string of length 1
– iachar gives
the integer ASCII code of character c
• achar(i)
– ASCII
character corresponding to integer i
• llt(a,b)
– compares
strings a and b using ASCII code
– result is .true.
if a < b in ASCII
– result is
independent of machine
– used instead
of relational operators
• similar
other functions for comparison
• lle(a,b),
lge(a,b), lgt(a,b)
• many
other functions also available
Example
• replace
two or more blanks occurring consecutively in a line by a single blank, remove
initial blanks and print the line
• repeat
for new lines till a blank line is found
• lines
assumed to contain at most 80 characters ( read from terminal)
• idea
– copy
nonblank portions to output line with one blank in between
Example
Program
program remove_blanks
implicit none
character(len=80) :: line
integer :: i, j, k
newline : do
read *, line
if ( line == “ ”) exit
i = 1 ; j = 1
! line read till position i-1 and correctly
filled till j-1
position : do
if ( i > 80) exit ! all input has been seen
if
(line(i:i) == “ ”) then
i = i + 1 ! skip blank
else
k = i
do ! find next blank
character or end of string
if (k == 80 .or. line(k:k) == “ ”)
exit
k = k+1
end do
! copy nonblank portion to correct position
line(j:j+k-i) = line(i:k)
j = j + k – i + 1
i = k + 1
endif
end do position
line(j:) = “ ” ! blank the remaining part of line
print *, line
end do newline
end program remove_blanks ! run it and see
Summary
• character
type variables can be used to store non-numeric data
• possible
values of a character variable are strings of characters of a fixed length
• a
fixed set of allowed characters is used
• ASCII
code for representing characters
• substrings
of character variables may be used
• concatenation
is the only operation
• character
variables may be compared using relational operators
– comparison
depends on character codes
– comparison
using lexicographic ordering
• several
intrinsic functions are available for manipulating characters