        character line*120,coded*120,lkey*26,cl*1
        character text(20000)*1,code(20000)*1
        integer ntext(20000),nmat(26,26),key(26),key2(26)
        integer ntext2(20000),ntext3(20000),ikey(26)

        do 48 i=1,26
            do 48 j=1,26
               nmat(i,j)=0
 48            continue
        do 49 i=1,120
          coded(i:i)=' '
          line(i:i)=' '
  49    continue

       
        n=0
        open(8,file="Cipher.7.ABC",form="formatted")
  1     read(8,1000,end=2)line
        write(*,1000)line
c       do 47 i=1,120
c 47      write(*,*)line(i:i),ichar(line(i:i))
1000    format(a120)
        do 15 i=1,120
          nl=ichar(line(i:i))
          if(nl.ge.97.and.nl.le.122)then
            nl=nl-32
          end if
          if(nl.ge.65.and.nl.le.90)then
            n=n+1
            text(n)=char(nl)
            ntext(n)=nl-64
c
c       a=1,...z=26
c
          end if
c       write(*,*)line(i:i),i,n,text(n),nl
  15    continue
        go to 1

  2     close(8)

        write(*,2000)(text(i),i=1,n)
 2000   format(1x,26a1)

        do 56 i=1,n
          nc=mod(i-1,26)+1
          nmat(nc,ntext(i))=nmat(nc,ntext(i))+1
c          write(*,*)i,nc,ntext(i),nmat(nc,ntext(i))
   56    continue

        do 57 i=1,26
          write(*,*)(nmat(i,j),j=1,26)
          jmax=1
          nmax=nmat(i,1)
          do 58 j=2,26
             if(nmat(i,j).gt.nmax)then
                jmax=j
                nmax=nmat(i,j)
             end if
 58          continue
         write(*,*)"Column ",i," Most common letter is ",
     &              char(jmax+64)," (",nmax," )"
 57      continue

                write (*,*)'This programme reads the E guesses from the'
     &     //' file RotMono2.key'
        open(8,file='RotMono2.key',form='formatted')
        read(8,*)lkey
        do 46 i=1,26
          nl=ichar(lkey(i:i))
          if(nl.ge.97)then
            nl=nl-32
          end if
c         write(*,*)"*** ",i,nl,nl-64,ncase(i)
          key(i)=nl-64
  46    continue
        write(*,*)'Give step length (N) '
        read(*,*)ng

        do 23 i=1,26
          j=mod(5+ng*(i-1)-1,26)+1
          key2(j)=key(i)
 23     continue
        do 24 i=1,26
           write(*,*)i,key2(i)
 24        continue

        do 25 i=1,26
           ikey(i)=0
 25        continue
        do 26 i=1,26
           ikey(key2(i))=i
 26    continue
 55    do 30 i=1,26
           write(*,*)i,ikey(i)
 30        continue

        do 27 i=1,n
           ntext2(i)=ikey(ntext(i))
 27        continue
        do 28 i=1,n
           ntext3(i)=mod(ntext2(i)-ng*(i-1)-1,26)+1
           if(ntext3(i).le.0)ntext3(i)=ntext3(i)+26
           if(ikey(ntext(i)).eq.0)ntext3(i)=-32
c           write(*,*)ntext(i),ntext2(i),ntext3(i)
 28        continue

        write(*,3000)(char(ntext3(i)+64),i=1,n)
 3000   format(1x,80a1)

        write(*,*)'Which letter number needs correcting?'
        read(*,*)nc
        write(*,*)'Currently ',char(ntext3(nc)+64),
     &               '. What should it be?'
        read(*,*)cl
        ncl=ichar(cl)-64
        n1=mod(ncl+ng*(nc-1)-1,26)+1
        write(*,*)char(n1+64),' (',n1,') was enciphered as ',
     &         text(nc),' (',ntext(nc),')'

        ikey(ntext(nc))=n1

        go to 55

        end
