PROBLEM 2. Finding patterns in strings %----------------------------------------
ID: 3623537 • Letter: P
Question
PROBLEM 2. Finding patterns in strings
%--------------------------------------------------------------------------
%
% Function Name: myStrFindI
% Inputs (2): - (char) any string
% - (char) a pattern to find within the first input string
% Outputs (1): - (double) a list of starting indices where the pattern
% occurs in the string, ignoring case
%
% Function Description:
% Given a string and a string pattern to search for within the first
% string, write a function called 'myStrFindI' that will return the
% starting indices of any occurrences of the pattern, ignoring case, in
% the given string, similar to the functionality of strfind(). As an
% example, if the input string was 'nomNomNomnom' and you were searching
% for the pattern string 'nom' in the input, your function would return
% the vector [1 4 7 10] to indicate the indices at which the pattern
% 'nom' appears in the first input string, regardless of case.
%
% Notes:
% - You may *NOT* use the strfind() function in your code.
% - Be sure to consider cases in which the second input string is longer
% than the first string.
%
% Test Cases:
% str1 = 'Ramblin'' Wreck from Georgia Tech';
% str2 = 'aNd a HeLlUva eNgInEEr.';
%
% out1 = myStrFindI(str1, 'ec');
% out1 => [12, 30]
%
% out2 = myStrFindI(str2, 'A');
% out2 => [1, 5, 13]
%
% out3 = myStrFindI(str2, 'aNd a HeLlUva eNgInEEr!!');
% out3 => []
Explanation / Answer
%MAIN FUNCTION
function [ resInd ] = myStrFindI(str1,str2 )
% Inputs (2): - str1 any string
% - str2 a pattern to find within the first input string
% Outputs (1): resInd- (double) a list of starting indices where the pattern
% occurs in the string, ignoring case
L1=numel(str1);L2=numel(str2);
if(L2<=L1)
str11=str2lc(str1);%str1 to lower case
str21=str2lc(str2);%str2 to lower case
cFirst=str21(1); %first char in str 2
%find indices which are suspected to be in answer
%i.e. they coincie with the first char in str1
mask=((str11-cFirst)==0);
ind=find(mask);
j=0;
%preallocate memory for speeding up the application
resInd=zeros(size(ind));
for i=ind %for each " suspected" index
if(i+L2-1<=L1) %if it is not too close to the end of str1
if cmpStrI(str11(i:i+L2-1),str21) %compare to str2
j=j+1; %if passed the test, add to the final result
resInd(j)=i;
end
end
end
%final polishing of results: cutting zero indices
if(j==0)
resInd=[];
elseif(j<numel(ind))
resInd(j+1:numel(ind))=[];
end
else %if second string is longer then the first one
resInd=[];
end
%SUBROUTINES
% the subroutines may be replaced with
% matlab built in functions
function [b]=cmpStrI(str1,str2)
%input:
% str1,str2: strings
%output: true if they are "equal", false o/w
bEq=false;
L=numel(str1);
if(L==numel(str2))
strC=str1-str2;
bEq=(sum(strC)==0);
end
b=bEq;
function [strLC]=str2lc(str)
%input: string str
%output: string str in low case
%example: "AaAbB" results "aaabb"
bUC= str>='A' & str<='Z';%find upper case letters
iUC=find(bUC); %find their indices
strLC=str;
strLC(iUC)=strLC(iUC)+('a'-'A');%to lower case
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.