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

Given the start of a program: paragraph = \'Mary had a little lamb. Its fleece w

ID: 3631440 • Letter: G

Question

Given the start of a program:
paragraph = 'Mary had a little lamb. Its fleece was white as snow. And every where that Mary went, the lamb was sure to go';

search_string = 'lamb';
Write a loop that finds the starting index of all occurrences of the string 'lamb', i.e. character 19 and 91 Note: you can determine if part of the paragraph matches 'lamb' with the strcmp function, e.g. strcmp(paragraph (3:6),'lamb') would return false, but strcmp(paragraph(19:22),'lamb') would return true Your answer should work for any search string and paragraph.

Explanation / Answer

function y=findstring(paragraph,search_string);

N = length(search_string); % length of the input string

Z = length(paragraph);

J = 1; % count for output

y = [];

for I = (1:(Z-N+1)),

    if (strcmp(paragraph(I:(I+N-1)),search_string)),

        y(J) = I;

        J = J+1; %increment J

    end

end

Just copy and paste this into a new m-file (File->New->M-file) and save it as "findstring.m".

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote