        character code(20000)*1,text(20000)*1,dig(100)*2,key(100)*2
        character alph(26)*1
        integer count(26,26)

        open(8,file="Playfair.data")
        n=0
  1     read(8,1000,end=2)(code(60*n+i),i=1,60)
1000    format(60a1)
        n=n+1
        go to 1
  2     close(8)
        write(*,*)n+1,' lines of code read.'
        do 3 i=60,1,-1
          if(code(i).eq." ")j=i-1
  3     continue
        nc = 55*n+j
        write(*,*)'The code has ',nc,' characters.'
        write(*,1000)(code(i),i=1,nc)

        do 4 i=1,nc,2
          j1 = ichar(code(i))-64
          j2 = ichar(code(i+1))-64
          count(j1,j2)=count(j1,j2)+1
  4     continue

        do 5 i=1,26
          alph(i)=char(64+i)
  5     continue

        write(*,1002)(alph(ic),ic=1,9),(alph(ic),ic=11,26)
1002    format(1x,25(2x,1a))
        do 20 ir=1,9
        write(*,1001)alph(ir),(count(ir,ic),ic=1,9)
     &            ,(count(ir,ic),ic=11,26)
 20     continue
        do 21 ir=11,26
        write(*,1001)alph(ir),(count(ir,ic),ic=1,9)
     &            ,(count(ir,ic),ic=11,26)
 21     continue
1001    format(1a,25(1x,i2))

  8     write(*,*)'Give digraph guesses:'
        i = 1
 31     read(*,*,end=30)dig(i),key(i)
        if(dig(i).eq.'ZZ')then
          i=i-1
          go to 30
        end if
        i=i+1
        go to 31

 30     write(*,*)i,' guesses read.'
        nk = i

        do 10 i=1,nc,2
          text(i)=code(i)
          text(i+1)=code(i+1)
          do 11 j=1,nk
            if(code(i).eq.dig(j)(1:1).and.
     &         code(i+1).eq.dig(j)(2:2))then
              text(i)=key(j)(1:1)
              text(i+1)=key(j)(2:2)
            end if
  11      continue
 10     continue

        write(*,*)'Latest guess is:'
        write(*,1000)(text(i),i=1,nc)


        go to 8
        end
