function text = transform(top, bottom, text, show)
% TRANSFORM: transform TEXT according to key with given TOP and BOTTOM lines
%   TEXT = TRANSFORM(TOP,BOTTOM,TEXT): return transformed string TEXT
%   TRANSFORM(TOP,BOTTOM,TEXT): print transformed string TEXT
%   TEXT = TRANSFORM(TOP,BOTTOM,TEXT,SHOW) or TRANSFORM(TOP,BOTTOM,TEXT,SHOW):
%          read strings from file TEXT and return/print at most SHOW lines
%
% note: assume TOP, BOTTOM contain only letters

% canonicalize key (throw away top since we know it is sorted)     
    top = lower(top) - 'a' + 1; bottom = lower(bottom);
    bottom(top) = bottom;
% figure out whether we're dealing with a string or a file:
% set FID to the file id or -1 and LINE to next string to process
    if nargin>3, fid = fopen(text); line = fgetl(fid);
    else fid = -1; show = 1; line = text; end
    text = [];
% transform text
    while show>0 & ischar(line)
        show = show-1;
        bias = line; line = lower(line); bias = bias-line;
        line(isletter(line)) = bottom(line(isletter(line)) - 'a' + 1);
        line = char(line+bias);
        if nargout == 0, disp(line); else text = [text ; line]; end
	if fid>0, line = fgetl(fid); end
    end