!------------------------------------------------------------------------------
!
!  Program name:
!
!    convert_case
!
!  Purpose:
!
!    This program shows how to use DO and CASE control constructs. 
!    It reads in a character string, changes the case of letters and 
!    writes out new string.
!
!  Note:
!
!    Need to know the difference in the collation sequence of the upper 
!    and lower case characters - use position of upper and lower case A: 
!
!                        IACHAR('A') - IACHAR('a')
!
!------------------------------------------------------------------------------

PROGRAM convert_case

  IMPLICIT NONE

  CHARACTER (LEN = 80) :: string
  INTEGER :: i, upper_to_lower, len_string

  upper_to_lower = IACHAR("a") - IACHAR("A")

  WRITE (*,*) 'Please enter string of up to 80 characters'

  ! Read in string (up to 80 characters):
  READ (*,'(A)') string

  ! Find length of string excluding trailing blanks
  len_string = LEN_TRIM(string)

  DO i = 1, len_string

    SELECT CASE (string(i:i))

      CASE ('A':'Z')            ! Upper case character found:
        string(i:i) = ACHAR(IACHAR(string(i:i)) + upper_to_lower)

      CASE ('a':'z')            ! Lower case character found:
        string(i:i) = ACHAR(IACHAR(string(i:i)) - upper_to_lower)
                                !  No change for any other characters
    END SELECT

  END DO

  ! Write out converted string
  WRITE (*,'(A)') string

END program convert_case


