Docsity

Character String Manipulation - Fortran Programming - Lecture Notes, Study notes of Computer Programming

Toggle sidebar
bg1
Character String Manipulation
CHARACTER Declaration:
• The declaration of CHARACTER variables and parameters
is done as follows (see sections 2.5 for details):
CHARACTER * n symbolic-name-list * m
Length of CHARACTER Data:
• The length of a CHARACTER constant is the number of
characters enclosed between the beginning and ending
apostrophes (counting each pair of enclosed apostrophes as
one).
• The length of a CHARACTER variable is specified when
the variable is declared.
Moving data into a CHARACTER variable
• When using a READ * statement or an assignment
statement. The value is either
• truncated or
• padded with blanks on the right, if needed,
to match the length of the variable.
• When using a formatted READ statement with an Aw edit
descriptor, the value is either:
• padded on the right with blanks or
• truncated from the left (yes, left), if needed,
to match the length of the variable.
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Character String Manipulation - Fortran Programming - Lecture Notes and more Study notes Computer Programming in PDF only on Docsity!

Character String Manipulation

CHARACTER Declaration:

  • The declaration of CHARACTER variables and parameters is done as follows (see sections 2.5 for details):

CHARACTER * n symbolic-name-list * m

Length of CHARACTER Data:

  • The length of a CHARACTER constant is the number of characters enclosed between the beginning and ending apostrophes (counting each pair of enclosed apostrophes as one).
  • The length of a CHARACTER variable is specified when the variable is declared.

Moving data into a CHARACTER variable

  • When using a READ * statement or an assignment statement. The value is either - truncated or - padded with blanks on the right, if needed, to match the length of the variable.
  • When using a formatted READ statement with an Aw edit descriptor, the value is either: - padded on the right with blanks or - truncated from the left (yes, left), if needed, to match the length of the variable.

ASCII

  • The American Standard Code for Information Interchange (ASCII, pronounced “askey”) is used on most computers for representing characters internally.
  • Originally, ASCII used 7 bits to represent 128 different characters including the usual graphic characters (A, ..., Z, a, ... z, !, @, #, $, etc.) and a number of special characters (bell, enter, space, tab, etc.).
  • With the advent of personal computers, the ASCII character set has been extended to 8 bits (1 byte) and contains 256 different characters.

STR1(3 : 7) = 'Sarah' STR2(1:3) = ‘FORTRAN’ STR2(4:8) = ‘HI’

  • The length of a substring is equal to the ending-position minus the starting-position plus one.
  • If the starting position of a substring is one, it may be omitted.

variable-name ( 1 : ending-position ) → variable-name ( : ending-position )

If the ending position is the end of the variable, it may also be omitted.

variable-name ( starting-position : length ) → variable-name ( starting-position : )

Example:

MVNC(:5) ≡ MVNC(1:5) MVNC(20:) ≡ MVNC(20:31)

Declaring CHARACTER Variables:

  • When a parameter or a constant expression is used to determine the length of a CHARACTER variable, the parameter or expression must be enclosed in parenthesis.

CHARACTER * ( constant-expression ) list-of-variable-names

STR3(1:10) = STR

STR3(15:40) = STR1 // ‘wow’

PROGRAM WORDS C Print each word in a sentence.

C Declarations. INTEGER LENGTH CHARACTER *1 BLANK PARAMETER (LENGTH = 80, BLANK = ' ') CHARACTER *(LENGTH) SENTNC INTEGER FIRST, NEXT

C Enter data string. PRINT *, 'Enter a string:' READ '(A)', SENTNC

C Print each word (the characters between the blanks) FIRST = 1 DO 10 NEXT = 1, LENGTH IF (SENTNC(NEXT : NEXT) .EQ. BLANK) THEN PRINT *, SENTNC(FIRST : NEXT) FIRST = NEXT + 1 ENDIF 10 CONTINUE

C Print the last word if it is not yet printed. IF (SENTNC(LENGTH : LENGTH) .NE. BLANK) THEN PRINT *, SENTNC(FIRST : LENGTH) ENDIF

C Exit program. STOP END 

The Fortran library function CHAR is used to determine the character in a specified position of the computer's collating sequence.

CHAR ( integer-position ) → single-character

PROGRAM COLLAT C Prints the relative position in the collating sequence C of all characters in the FORTRAN character set.

C Declarations. INTEGER SETSIZ PARAMETER (SETSIZ = 49) INTEGER POS CHARACTER *(SETSIZ) CHRSET

C FORTRAN character set. DATA CHRSET /' ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-*/()=$,.'':'/

C Print each character and its relative position. PRINT *, 'Collating sequence positions for',

  • ' FORTRAN characters' PRINT * PRINT *, 'Character Position' DO 10 POS = 1, SETSIZ PRINT '(5X, A, 11X, I3)',
  • CHRSET(POS : POS), ICHAR(CHRSET(POS : POS)) 10 CONTINUE

C Exit program. STOP END

String Length and Search Functions

The Function LEN:

  • The Fortran library function LEN is returns the length of a CHARACTER value (constant, variable, or expression).
  • The length of a CHARACTER variable is independent of the data stored in it.

LEN ( character-value ) → integer-length

Dummy CHARACTER Arguments

  • Subroutines and functions may have arguments of type CHARACTER.
  • These arguments may have lengths which vary from o ne call to the next.
  • These dummy arguments are declared as follows:

CHARACTER * ( * ) symbolic-name-list

  • If needed, the actual length of the dummy argument may be determined in the subroutine or function by using the library function LEN.

The Function GETLEN INTEGER FUNCTION GETLEN (STRING) C Determines length of string excluding any blank padding. C Pre : STRING is defined. C Post: The function result is the actual string length C and includes only the characters preceding the padding.

C Argument Declarations. CHARACTER () STRING

C Local Declarations. CHARACTER *1 BLANK PARAMETER (BLANK = ' ') INTEGER NEXT

C Start with the last character and find the first nonblank. DO 10 NEXT = LEN(STRING), 1, - IF (STRING(NEXT : NEXT) .NE. BLANK) THEN GETLEN = NEXT ENDIF 10 CONTINUE

C All characters are blanks. GETLEN = 0

C Exit function. RETURN END

PROGRAM AINT C Replaces AIN'T with IS NOT.

C Declarations. CHARACTER *80 SENTNC, COPY INTEGER POSIT

C Enter data string. PRINT *, 'Old sentence:' READ '(A)', SENTNC

C Find each occurrence of AIN'T in SENTNC. POSIT = INDEX(SENTNC, 'AIN''T') DO WHILE (POSIT .NE. 0) COPY = SENTNC( : POSIT-1) //

  • 'IS NOT' // SENTNC(POSIT+LEN('AIN''T') : ) SENTNC = COPY POSIT = INDEX(SENTNC, 'AIN''T') ENDDO

C Print results. PRINT *, 'New sentence:' PRINT *, SENTNC

C Exit program. STOP END 

Common Programming Errors

Two special problems involved with using CHARACTER data:

  • In a CHARACTER assignment statement, none of the character positions being defined in the character-variable to the left of the assignment operator may be part of the character-expression to the right of the assignment operator.
  • When using a substring, the starting-position must be greater than or equal to one and less than or equal to the ending-position, while the ending-position must be greater than or equal to the starting position and less than or equal to the length of the string.