PROGRAM Caesar_Cipher
!------------------------------------------------------------------------
! This program encrypts a character string using the Caesar cipher
! scheme.  Variables used are the following:
!   Message : message to be encrypted
!   Key     : integer to be added in encrypting the message
!   Code    : numeric code for a symbol in Message
!   I       : counter
!
! Input:  Message and Key
! Output: User prompts and Message (coded)
!------------------------------------------------------------------------

  IMPLICIT NONE
  CHARACTER(80) :: Message
  INTEGER :: Key, Code, I

  WRITE (*, '(1X, A)', ADVANCE = "NO") "Enter message: "
  READ '(A)', Message
  WRITE (*, '(1X, A)', ADVANCE = "NO") "Enter key: "
  READ *, Key

  I = 0
  DO
     ! Get next character in message
     I = I + 1
     IF (I > LEN_TRIM(Message)) EXIT
     ! If end of string, stop

     ! Otherwise continue coding characters
     Code = MOD((ICHAR(Message(I:I)) - 32 + Key), 95) + 32
     Message(I:I) = CHAR(Code)
  END DO

  PRINT *, "Coded message: ", Message

END PROGRAM Caesar_Cipher
