FORTRAN Computer Games


ROT-13

ROT-13 is a simple substitution cipher. This implementation in FORTRAN 77 reads a line from user input and encodes/decodes it.

Gameplay

Encoding a message:

ENTER MESSAGE: FORTRAN, THE GREATEST OF THE PROGRAMMING LANGUAGES!
SBEGENA, GUR TERNGRFG BS GUR CEBTENZZVAT YNATHNTRF!

Decoding a message:

ENTER MESSAGE: SBEGENA, GUR TERNGRFG BS GUR CEBTENZZVAT YNATHNTRF!
FORTRAN, THE GREATEST OF THE PROGRAMMING LANGUAGES!

Functions & Subroutines

A utility function is required that returns the (trimmed) length of a string:

RESULT = LENGTH(STRING)
Returns the trimmed length of given string (CHARACTER).

In Fortran 90, we can call the intrinsic function LEN_TRIM() instead.

Program Listing

Copy and save the program as rot13.f.

C     ******************************************************************
C
C     BASIC ROT-13 ENCODER/DECODER IN FORTRAN 77
C
C     ******************************************************************
      PROGRAM ROT13
      INTEGER       LENGTH
      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, LENGTH(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 LENGTH(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 LENGTH = I
      END

Build Instructions

UNIXFlang/F18$ flang -o rot13 rot13.f
GNU Fortran$ gfortran -o rot13 rot13.f
Intel Fortran Compiler$ ifort -o rot13 rot13.f
Win32Digital/Compaq Visual Fortran> fl32.exe rot13.f /Fe=rot13.exe

References


Home