ROT-13 is a simple substitution cipher. This implementation in FORTRAN 77 reads a line from user input and encodes/decodes it.
A utility function is required that returns the (trimmed) length of a string:
RESULT = LTRIM(STRING)
CHARACTER
).In Fortran 90, we can call the intrinsic function
LEN_TRIM()
instead.
Copy and save the program as rot13.f
.
C ******************************************************************
C
C BASIC ROT-13 ENCODER/DECODER IN FORTRAN 77
C
C ******************************************************************
PROGRAM ROT13
INTEGER LTRIM
CHARACTER*256 MSG
INTEGER I, J, ISTAT
C
C READ INPUT MESSAGE.
C
PRINT 100
READ (*, 200, IOSTAT=ISTAT) MSG
IF (ISTAT .NE. 0) STOP
100 FORMAT ('ENTER MESSAGE: ',$)
200 FORMAT (A)
C
C PRINT EACH CHARACTER.
C
DO 10 I = 1, LTRIM(MSG)
J = ICHAR(MSG(I:I))
IF (J .GT. 64 .AND. J .LT. 78) THEN
J = J + 13
ELSE IF (J .GT. 77 .AND. J .LT. 91) THEN
J = J - 13
ELSE IF (J .GT. 96 .AND. J .LT. 100) THEN
J = J + 13
ELSE IF (J .GT. 109 .AND. J .LT. 123) THEN
J = J - 13
END IF
PRINT 300, CHAR(J)
10 CONTINUE
300 FORMAT (A1,$)
PRINT *
END
C ******************************************************************
INTEGER FUNCTION LTRIM(STRING)
C
C RETURNS LENGTH OF TRIMMED STRING, LIKE LEN_TRIM() IN FORTRAN 90.
C
CHARACTER*(*) STRING
INTEGER I
DO 10, I = LEN(STRING), 1, -1
IF (STRING(I:I) .NE. ' ') GOTO 20
10 CONTINUE
20 LTRIM = I
END
UNIX | Flang/F18 | $ flang -o rot13 rot13.f |
---|---|---|
GNU Fortran | $ gfortran -o rot13 rot13.f | |
Intel Fortran Compiler | $ ifort -o rot13 rot13.f | |
Win32 | Digital/Compaq Visual Fortran | > fl32.exe rot13.f /Fe=rot13.exe |