program letterdif(infile1,infile2,outfile,input,output);

{******************************
 *        Difference          *
 *    Compares guessed text   *
 *     with corresponding     * 
 * sections of encrypted text *
 * Written by Michael Shuter. *
 ******************************
}

var
ch1,ch2: char;
diff,temp,counter: integer;
infile1,infile2,outfile: text;
error,stop,debug:boolean;
choice,option:char;

begin
	reset(infile1);  {reads from encrypted (infile1) and guessed text (infile2)}
	reset(infile2);
	debug:=false;
	rewrite(outfile);
	repeat
		write('Do you want debug on (y/n): ');
		readln(option);
	until option in ['y','n','Y','N'];
	if option in ['y','Y'] then
		debug:=true; {sets debug onn/off}
	assert(((option in ['y','Y']) and (debug=true)) or ((option in ['n','N']) and (debug=false)));
	counter:=0;
	error:=false;
	stop:=true;
	repeat 

		read(infile1,ch1); {reads two characters at once,  if errors in typing guessed text big problems can result}
		read(infile2,ch2);
		if ch1 in ['A'..'Z'] then {converts capitals to lowercase}{PRE:ch1 is a capital}
		begin
			temp:=ord(ch1)+32;
			ch1:=chr(temp);{POST: ch1 is lowercase}
		end;{if}
		if ch2 in ['A'..'Z'] then {converts capitals to lowercase}{PRE: ch2 is a capital}
		begin
			temp:=ord(ch2)+32;
			ch2:=chr(temp);{POST: ch2 is lowercase}
		end;{if}
		assert (not(ch1 in ['A'..'Z']) and not(ch2 in ['A'..'Z']));
		if debug=true then
			write(outfile,ch1,' to ',ch2);
		if stop= true then{tests for errors but only once}
		begin
			if (ch1 in ['a'..'z']) and not (ch2 in ['a'..'z']) then
				error:=true;			
			if (ch2 in ['a'..'z']) and not (ch1 in ['a'..'z']) then
				error:=true;
		end;{if}
		if (not(ch1 in ['a'..'z'])) and (ch1=ch2) then {writes normal characters as they are}
		begin
			write(outfile,ch1);
			if ch1=' ' then
				write(outfile,ch1);
		end{if}
		else
		begin {compares both letters in they are in the alphabet}
			if (ch1 in ['a'..'z']) and (ch2 in ['a'..'z']) then
			begin
				counter:=counter+1;
				diff:=(ord(ch2))-(ord(ch1));  {calculates difference}{PRE: -25<=diff<=25}
				if diff<0 then  {allows for complete loop ie.z to a }
					diff:=diff+26;{POST: 0<=diff<26}
				assert ((diff>=0) and (diff<26));
				write(outfile,diff:1,' ');
			end;{if}
		end;{else}
		if (eoln(infile1)) and (eoln(infile2)) then
		begin
			readln(infile1);
			readln(infile2);
			writeln(outfile);
		end;{if deals with eoln}
		if error=true then {shows error message}
		begin
			repeat {reads whether or not to continue}
				writeln('An error has been detected. The character types in the two files do not match up.');
				write('The number of letters compared so far is ',counter:1,'.  Do you want to (c)ontinue anyway or (s)top : ');
				readln(choice);
			until choice in ['c','s','C','S'];
			if (choice='c') or (choice='C') then 
			begin
				error:=false;
				stop:=false;
			end;{if}
		end;{if}
	until (eof(infile1)) or (eof(infile2))or (error=true); {stops the comparison}
	if not(((eof(infile1))and (eof(infile2))) or ((not eof(infile1)) and (not eof(infile2)))) then {final error test}
		writeln('The files were of different lengths');
end.{letterdif}

