function letters = mailMerge(send_fname,recp_fname,tmpl_fname,ZIP_fname)
% Perform a mail merge given the data files
% send_fname: a string that gives the complete path to the sender file
% recp_fname: a string that gives the complete path to the recipient file
% tmpl_fname: a string that gives the complete path to the template file
% ZIP_fname: a string that gives the complete path to the ZIP code database
% letters: a cell array, each cell of which contains one string that is the
% content of the letter to a recipient with valid ZIP code
% If the sender's ZIP code is invalid, letters is the empty cell array.

%%% IMPLEMENT ME (remove the stub below) %%%
letters = {};

function Z = MakeZipcode(ZIP,city,state,county)
% Return a Zipcode structure Z such that
% Z.ZIP is assigned the string in ZIP
% Z.city is assigned the string in city
% Z.state is assigned the string in state, and
% Z.county is assigned the string in county

%%% IMPLEMENT ME (remove the stub below) %%%
Z = [];

function C = readZIP(ZIP_fname)
% Read ZIP code information from the given file.
% ZIP_fname: a string that gives the complete path to the file
% C: a structure array with component i being a struct containing the
% information of the ith ZIP code.

%%% IMPLEMENT ME (remove the stub below) %%%
C = [];

function ZIPinfo = findZIP(C,ZIP)
% Return the information of the specified ZIP code
% C: a structure array containing the information of the ZIP codes
% ZIP: the ZIP code (length-5 string) to be looked up
% ZIPinfo: a structure containing of the information of ZIP
% If the ZIP code is not found, ZIPinfo is the empty vector.

%%% IMPLEMENT ME (remove the stub below) %%%
ZIPinfo = [];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DO NOT MODIFY ANYTHING BELOW THIS LINE %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function S = readSender(send_fname)
% Read sender's information from the given file having the specified format.
% send_fname: a string that gives the complete path to the file
% S: a structure containing the information of the sender
% The function assumes there are no file format errors.

C=File2Cell(send_fname);
S=readPerson(C,1);

function RL = readRecipients(recp_fname)
% Read recipients' information from the given file having the specified format.
% recp_fname: a string that gives the complete path to the file
% RL: a structure array with component i being a struct containing the
% information of the ith recipient
% The function assumes there are no file format errors.

C=File2Cell(recp_fname);

% read number of recipients
numRecp=str2double(C{1});

line=2;
% read each recipient's information
for i=1:numRecp
[RL(i),line]=readPerson(C,line);
end

function [PersonInfo,line] = readPerson(C,line)
% Read the information of a person from the given cell array beginning at the
% specified line.
% C: a cell array containing information
% line: (parameter) First line to read
% PersonInfo: a structure containing the person's information
% line: (return value) The line after the person's record
% The function does not assume file format errors.

% extract first and last names
name=C{line};
lenName=length(name);
spacePos=strfind(name(1:lenName-1),' ');
if isempty(spacePos)
firstName=name;
lastName='';
else
firstName=name(1:spacePos(1)-1);
lastName=name(spacePos(1)+1:lenName);
end

% extract address lines
numAddrLines=str2double(C{line+1});
line=line+2;
address=C{line};
for l=line+1:line+numAddrLines-1
address=sprintf('%s\n%s',address,C{l});
end

% extract ZIP+4 code
line=line+numAddrLines;
zip4=C{line};

PersonInfo= struct('first',firstName,'last',lastName,'addr',address, ...
'zip4',zip4);
line=line+1;