program morseovka_tam_a_zpet; {v t1 na kazdem radku znak ktery chci kodovat
 a hned vedle - bez mezery !!! - jeho kod, v t2 text co chci kodovat}

var a: array [1..50] of char;           { kodovane znaky }
    b: array [1..50] of string[5];      { jejich kody    }
    c,minuly: char;
    s: string;
    i, j: integer;
    hotovo: boolean;
    t1, t2, t3, t4 :text;

procedure DOMORSE;
  begin
    hotovo:=false;
    c:=' '; { mezera }
    while not eof(t2) do            { neseekovat !!! dela to pak blbosti }
      begin
        minuly:=c;
        hotovo:=false;
        if eoln(t2) then begin readln (t2); c:=' '; end else read(t2,c);
        c:=upcase(c);
        for j:=1 to i do
          if a[j]=c then begin write(t3,b[j],chr(124)); hotovo:=true; break; end; {124 oddeluje kazde pismeno}
        if (not hotovo) and (c=' ') and (minuly<>' ') then write (t3,chr(124)); {a dve 124 oddeluji kazde slovo }
        if (not hotovo) and (ord(c)>32) then write(t3,chr(215),chr(124)); {215 je znak pro ktery nemam kod}
      end;                                 { 215 a 124 jsou paradni znaky }
    close(t2);  close(t3);
  end;

procedure AZPATKY;
  begin
    assign (t3,'K30morse.txt');  reset (t3);
    s:='';  c:=' ';
    while not eof(t3) do
      if eoln(t3)
        then readln(t3)
        else
          begin
            minuly:=c;
            s:='';
            read(t3,c);
            while (ord(c)<>124) and (ord(c)<>215) do begin s:=s+c; minuly:=c; read(t3,c); end;
            if (ord(c)=124) and (ord(minuly)=124) then write (t4,' ');
            if ord(c)=215 then write (t4,c);
            if s<>'' then
              for j:=1 to i do
                if b[j]=s then begin write(t4,a[j]); break; end;
          end;
    close(t3);  close(t4) ;
  end;

begin
  assign (t1,'kodK30.txt');    reset (t1);
  assign (t2,'textK30.txt');   reset (t2);    {read(s); assign (t2,s); reset (t2); }
  assign (t3,'K30morse.txt');  rewrite (t3);  { by slo nacitat jmeno souboru ktery chci kodovat z klavesnice }
  assign (t4,'K30alpha.txt');  rewrite (t4);  { treba by to chtel kryl }

  i:=0;

  while not seekeof(t1) do
    begin
      readln(t1,a[i+1],b[i+1]);
      a[i+1]:=upcase(a[i+1]);     { na velikosti zalezi }
      inc (i);
    end;
  close (t1);                     { v a,b znaky + kody, i - kolik jich je }

  DOMORSE;
  AZPATKY;

end.
