Individual characters could be read into integer variables (or elements of integer arrays) using the format specifier A1, manipulated at will, then printed using the A1 format specifier.
For example (using the BESM-6 clone of the CDC 1604 FORTRAN compiler),
PROGRAM MAIN
INTEGER STR(80)
READ 10,STR
10 FORMAT(80A1)
2 PRINT 11,STR
11 FORMAT(’ READ: ’, 80A1)
3 DO 2 I = 1,80
4 IF (STR(81-I).NE.1H )
5 - GOTO 3
6 2 CONTINUE
7 3 I=81-I
8 PRINT 12,I
12 FORMAT(’ STRING LENGTH = ’, I3)
9 DO 4 J=1,I
10 K=STR(J)
11 STR(J)=STR(81-J)
12 4 STR(81-J)=K
13 I=81-I
14 PRINT 13,(STR(J),J=I,80)
13 FORMAT(’ REVERSED: ’, 80A1)
END
*EXECUTE
READ: HELLO, WORLD!
STRING LENGTH = 14
REVERSED: !DLROW ,OLLEH
Also there were operators ENCODE
and DECODE
, said to be non-standard in FORTRAN 77 but provided for compatibility with older versions of FORTRAN.
They are somewhat analogous in functionality to the C library functions snprintf
and sscanf
, allowing to perform formatted I/O to/from packed character strings. For example, generating a format string on the fly and printing something using it could look like
PROGRAM MAIN
INTEGER STR(80),FMT(20)
READ 10,STR
10 FORMAT(80A1)
2 ENCODE (80, 11, FMT) STR
11 FORMAT (2H(’,20A1,5H’,I6))
3 PRINT FMT,123456
END
*EXECUTE
ARBITRARY PREFIX 123456
Here the input stream contained the line ARBITRARY PREFIX
which was read into an "unpacked character" array, then interpolated into an array containing "packed characters" using the ENCODE
statement to form a valid format string, then the newly formed packed character string was used as the format to print an integer.
CHARACTER
types were available in the language.