{ Author: Douglas Jones Purpose: Convert between normal text and substitution cypher text } PROGRAM mp3( input, output ); TYPE lineindex = 1..128; linelen = 0..128; line = ARRAY [lineindex] OF char; VAR sub: ARRAY [char] OF char; ch: char; PROCEDURE readline( VAR f: text; VAR buf: line; VAR len: linelen ); { read one line from f into buf, reporting the length in len } BEGIN len := 0; WHILE not( eoln( f ) ) DO BEGIN len := len + 1; read( f, buf[len] ); END; readln( f ); END; { readline } PROCEDURE readkey; { read the key file and use it to initialize the array sub } VAR key: text; old, new: line; oldlen, newlen: linelen; i: lineindex; ch: char; BEGIN reset( key, 'key' ); readline( key, old, oldlen ); readline( key, new, newlen ); IF oldlen <> newlen THEN writeln( 'key lines not same length!' ); FOR ch := chr( 0 ) TO chr( 127 ) DO sub[ch] := ch; FOR i := 1 TO oldlen DO sub[old[i]] := new[i]; END; { readkey } BEGIN { mp3 } readkey; WHILE not( eof ) DO BEGIN WHILE not( eoln ) DO BEGIN read( ch ); write( sub[ ch ] ); END; readln; writeln; END; END. { mp3 }