      subroutine repl(orb,o1,o2,yrepl)
      implicit real*8 (a-h,o-x,z),logical*1(y)
      character(*) orb
      character(*) o1,o2
      yw=.false.
      ll=len(orb)
      long=len_trim(orb)
      yrepl=.false.
      long1=len(o1)
      long2=len(o2)
      i=index(orb,o1)
      if(yw)print*,'entree repl',i,long
      if(yw)print*,'orb',orb(1:long)
      if(yw)print*,'o1',o1,long1
      if(yw)print*,'o2',o2,long2
      if(long+long2-long1>ll) then
         print*,'erreur dans repl, la ligne devient trop longue'
         print*,'ligne a modifier : ',trim(orb(1:long))
         print*,'String a remplacer : ',o1,long1
         print*,'String  de  remplacement : ',o2,long2
         print*,'longueur avant modif',long
         print*,'longueur 1er string',long1
         print*,'longueur 2eme string',long2
         print*,'longueur apres modif',long+long2-long1
         print*,'longueur disponible',ll
         print*,trim(orb)
         stop
      endif
      if(i.ne.0) then
         yrepl=.true.
         orb(i+long2:long+long2-long1)=orb(i+long1:long)
         if(long1.gt.long2) then
            orb(long+long2-long1+1:long)=' '
         endif
         orb(i:i+long2-1)=o2
      endif
      if(yw)print*,'sortie repl'
      long=long+long2-long1
      if(yw)print*,'orb',orb(1:long)
      return
      end
      subroutine repl_num(orb,o1,o2,yrepl,num)
      implicit real*8 (a-h,o-x,z),logical*1(y)
      character(*) orb
      character(*) o1,o2
      yw=.false.
      ll=len(orb)
      long=len_trim(orb)
      yrepl=.false.
      long1=len(o1)
      long2=len(o2)
      print*,'orb0 ',trim(orb)
      do k=1,num-1
         i=index(orb,o1)
         orb(i:i+len_trim(o1)-1)='#'
      enddo   
      print*,'orb1 ',trim(orb)
      i=index(orb,o1)
      do k=1,num-1
         j=index(orb,'#')
         orb(j:j+len_trim(o1)-1)=trim(o1)
      enddo   
      print*,'orb2 ',trim(orb)
      
      if(yw)print*,'entree repl',i,long
      if(yw)print*,'orb',orb(1:long)
      if(yw)print*,'o1',o1,long1
      if(yw)print*,'o2',o2,long2
      if(long+long2-long1>ll) then
         print*,'erreur dans repl, la ligne devient trop longue'
         print*,'ligne a modifier : ',trim(orb(1:long))
         print*,'String a remplacer : ',o1,long1
         print*,'String  de  remplacement : ',o2,long2
         print*,'longueur avant modif',long
         print*,'longueur 1er string',long1
         print*,'longueur 2eme string',long2
         print*,'longueur apres modif',long+long2-long1
         print*,'longueur disponible',ll
         print*,trim(orb)
         stop
      endif
      if(i.ne.0) then
         yrepl=.true.
         orb(i+long2:long+long2-long1)=orb(i+long1:long)
         if(long1.gt.long2) then
            orb(long+long2-long1+1:long)=' '
         endif
         orb(i:i+long2-1)=o2
      endif
      if(yw)print*,'sortie repl'
      long=long+long2-long1
      if(yw)print*,'orb',orb(1:long)
      return
      end
       subroutine repl_all(orb,o1,o2,yrepl)
      implicit real*8 (a-h,o-x,z),logical*1(y)
      character(*) orb
      character(*) o1,o2
      yrepl2=.true.
      i=0
      do while (yrepl2)
         call repl(orb,o1,o2,yrepl2)
         i=i+1
      enddo
      yrepl=i>1
      return
      end
      
