Help with the Matlab code for this? Function Name: ottendorf Inputs: 1. (char) T
ID: 664399 • Letter: H
Question
Help with the Matlab code for this?
Function Name: ottendorf
Inputs:
1. (char) The beginning of the filenames of the “.txt” files being used as reference passages
2. (char) The name of a “.txt” file containing the cipher
Outputs:
1. (char) The secret message decoded using the cipher and reference passages
Function Description:
An Ottendorf—or book—cipher is a type of encoding method (referenced in the movie
National Treasure) in which sets of numbers correspond to letters in some set of reference
materials. In this variation, each letter will be encoded in the following manner:
<reference #>-<line #>-<word #>-<letter #>
For example, the cipher line 3-23-8-4 corresponds to the fourth letter of the eighth word
in the twenty-third line of the third reference text file. The first number would usually represent a
page number in a book, so you can think of each reference file as a single page of a whole
unified reference.
Write a function that takes a file of encoded letters (one letter’s sequence per line) and
outputs the message decoded using a given set of reference documents.
Notes:
The reference filenames will always be formatted as ‘<input1>_#.txt’. For example, if the
first input of the function were ‘hpLastPage’ and the current cipher line were 3-23-8-4,
you would be looking in the file called ‘hpLastPage_3.txt’.
The end of the cipher file will be marked with a line containing 0-0-0-0; blank (empty)
lines should be represented as spaces in the decoded message output, and lines of
punctuation in the cipher should be carried over to the output string as well.
You do not need to worry about finding blank lines or punctuation in the reference files;
no words with quotes, hyphens, etc. will be encoded.
Hints:
Don’t forget to close files as you finish using them.
You should only tokenize using spaces.
Remember that lines read in from a file are strings (the str2num() function may be
useful).
Explanation / Answer
prompt='Enter reference passage name';
ref_passage=input(prompt,'s')
prompt='Enter cipher line';
cipher_line=input(prompt,'s')
token_arr=strsplit(cipher_line,'-')
page_no=token_arr{1}
file_name=strcat(ref_passage,'_')
file_name=strcat(file_name,'.txt')
line_no=token_arr{2}
word_no=token_arr{3}
letter_no=token_arr{4}
file_id = fopen(file_name,'r');
i=0;
while ~feof(file_id)
i=i+1;
line = fgets(file_id);
if line==line_no
break
end
end
words_arr=strsplit(line)
target_word=words_arr{word_no}
target_letter=substring(target_word,letter_no,1)
disp(target_letter)
fclose(file_id);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.