Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Function Name: fixRefs % Inputs (2): - (char) A string representing the name of

ID: 3538030 • Letter: F

Question

Function Name: fixRefs

% Inputs (2): - (char) A string representing the name of a text

% file containing a report with numbered

% references

% - (char) A string representing the name of another

% text file containing a list of references

% and corresponding author names

% Outputs (0)

%

% Output Files (1): a .txt file containing the original report with the

% numbered references replaced by author names

%

% Function Description:

% Write a function called fixRefs that takes in the file name of a text

% file containing a report that has numbered in-text references and the

% file name of another text file with a numbered list of the author

% corresponding to each reference number. Your function should output a

% file that matches the original report file, except with the numbered

% references replaced by their matching author names from the references

% list. The new file should be named '[old report name]_final.txt'. For

% example, if the original report file name was 'Geography of Asia.txt',

% the output file name should be 'Geography of Asia_final.txt'.

%

% The numbered references in the report will be given in the format,

% '(reference#).', with parentheses around the number and a period

% following the right (close) parenthesis. For example:

%

% "The Sun is almost perfectly spherical (12). It

% consists of hot plasma interwoven with magnetic fields."

%

% When replacing a numbered reference with the author's name, the name

% should be inserted directly in the place of the number, maintaining the

% parentheses and period. The breaks between lines should be maintained

% as in the original report. For example:

%

% "The Sun is almost perfectly spherical (Snyder). It

% consists of hot plasma interwoven with magnetic fields."

%

% Each line in the reference list will be given in the format,

% 'reference#. author', with a single space between the period and the

% author's last name, for example:

%

% "1. Williams

% 2. Heller

% 3. Orwell"

%

% Notes:

% - The reference numbers are guaranteed to be at the end of a sentence,

% but they will never be the last thing at the end of a line.

% - Once used, a reference may appear again later in the report.

% Therefore, the references are not guaranteed to appear in numerical

% order in the report.

% - The report will not contain parentheses other than those used for the

% reference numbers.

% - There may be any number of references in the list.

% - Not all references in the list may be used in the report.

%

% Hint:

% - You may find the functions strtok(),strfind(), or strrep() helpful.

%

% Test Cases:

% fixRefs('Neurons.txt','Reflist1.txt')

% => file named 'Neurons_final.txt' and should look like

% 'Neurons_solution.txt'

%

% fixRefs('History of the Sandwich.txt','Reflist2.txt')

% => file named History of the Sandwich_final.txt' and should look

% like 'History of the Sandwich_solution.txt'

Explanation / Answer

function fixRefs(inp1,inp2)

oup=strtok(inp1,'.');

oup=strcat(oup,'_final.txt');

text=fileread(inp1);

fid = fopen(inp2);

tline = fgetl(fid);

while ischar(tline)

[num,auth]=strtok(tline);

num=strtok(num,'.');

num=strcat('(',num,')');

auth=strtok(auth);

auth=strcat('(',auth,')');

text=strrep(text,num,auth);

tline = fgetl(fid);

end

fclose(fid);

ofile=fopen(oup,'w');

fprintf(ofile,'%s',text);

fclose(ofile);

end