









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










CHARACTER Declaration:
CHARACTER * n symbolic-name-list * m
Length of CHARACTER Data:
Moving data into a CHARACTER variable
STR1(3 : 7) = 'Sarah' STR2(1:3) = ‘FORTRAN’ STR2(4:8) = ‘HI’
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:
CHARACTER * ( constant-expression ) list-of-variable-names
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',
C Exit program. STOP END
The Function LEN:
LEN ( character-value ) → integer-length
Dummy CHARACTER Arguments
CHARACTER * ( * ) symbolic-name-list
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) //
C Print results. PRINT *, 'New sentence:' PRINT *, SENTNC
C Exit program. STOP END
Common Programming Errors
Two special problems involved with using CHARACTER data: