Checking for Palindromes

Problem Statement

An array is a palindrome if it reads the same in both directions. For example,
3 5 7 2 4
is not a palindrome; however, the following one is.
4 2 6 2 4

Write a program that reads in an array and checks if it is a palindrome.

Solution

! --------------------------------------------------------------------
! PROGRAM  Palindrome:
!    This program checks to see if an array is a palindrome.  An array
! is a palindrome if they read the same in both directions.
! --------------------------------------------------------------------

PROGRAM  Palindrome
   IMPLICIT  NONE

   INTEGER, PARAMETER :: LENGTH = 30    ! maximum array size
   INTEGER, DIMENSION(1:LENGTH) :: x    ! the array
   INTEGER            :: Size           ! actual array size (input)
   INTEGER            :: Head           ! pointer moving forward
   INTEGER            :: Tail           ! pointer moving backward
   INTEGER            :: i              ! running index

   READ(*,*) Size, (x(i), i = 1, Size)  ! read in the input array
   WRITE(*,*)  "Input array:"           ! display the input
   WRITE(*,*)  (x(i), i = 1, Size)

   Head = 1                             ! scan from the beginning
   Tail = Size                          ! scan from the end
   DO                                   ! checking array
      IF (Head >= Tail)  EXIT           !   exit if two pointers meet
      IF (x(Head) /= x(Tail))  EXIT     !   exit if two elements not equal
      Head = Head + 1                   !   equal.  Head moves forward
      Tail = Tail - 1                   !   and Tail moves backward
   END DO                               ! until done

   WRITE(*,*)
   IF (Head >= Tail) THEN               ! if Head cross Tail, then we have
      WRITE(*,*) "The input array is a palindrome"     ! a palindrome
   ELSE                                 ! otherwise, it is not a palindrome
      WRITE(*,*) "The input array is NOT a palindrome"
   END IF

END PROGRAM  Palindrome
Click here to download this program.

Program Input and Output

If the input data consist of the following:
7
1 2 3 4 3 2 1
The out of the program is:
Input array:
1,  2,  3,  4,  3,  2,  1

The input array is a palindrome
If the input data consist of the following:
10
1 3 5 7 9 7 5 3 2 1
The out of the program is:
Input array:
1,  3,  5,  7,  9,  7,  5,  3,  2,  1

The input array is NOT a palindrome

Discussion