strfind, lower/upper, numel/length Find the number of occurrences of a substring
ID: 3148036 • Letter: S
Question
strfind, lower/upper, numel/length Find the number of occurrences of a substring in another string, ignoring case (case insensitive). Write a function count_substr that takes two input strings, . 1st - the string to be searched on, . 2nd - the substring which is the search term (the pattern), and returns the number of occurrences (a double scalar value). See examples below Note: The term substring means a string within another string Hints The string matching here is simple, you should notneed more than strfind along with lower/upper . Examples >> my-string 'Fish tacos are awesome. A |fisherman is fishing fish. '; >num-count_substr (my_string, 'FisH' num >> mystring tacos are awesome. A fisherman is fishing ' - pattern ' IS ' ; >>count - count substr (my string, pattern) countExplanation / Answer
clc; clear all; % Ask for first input from user str1 = input('Enter First String:','s'); % ask for another input from user str2 = input('Enter Second String:','s'); % Find occurane of second in first index=strfind(str1,str2); [x,y]=size(index); i=1; while(i<=y) %Print Index of pattern matching in String index(i) i=i+1; end without strfind clc; clear all; % Ask for first input from user str1 = input('Enter First String:','s'); % ask for another input from user str2 = input('Enter Second String:','s'); [x,y]=size(str1); [x2,y2]=size(str2); k=1; for i=1:y j=1; if(str1(i)==str2(1)) while( i+j<y+1 && 1+j<y2+1 && str1(i+j)==str2(1+j)) j=j+1; end if(j==y2) index(k)=i+j-y2 k=k+1; end end end clc; clear all; % Ask for first input from user str1 = input('Enter First String:','s'); % ask for another input from user str2 = input('Enter Second String:','s'); [x,y]=size(str1); [x2,y2]=size(str2); k=1; for i=1:y j=1; if(str1(i)==str2(1)) while( i+j<y+1 && 1+j<y2+1 && str1(i+j)==str2(1+j)) j=j+1; end if(j==y2) index(k)=i+j-y2 k=k+1; end end end Problem(B) Program to check the entered number is palindrome or not clc; clear all; str=input('Enter the string ','s'); [x,y]=size(str); j=0; mid=y/2; p=1; %checking string from start and end for i=1:mid if (str(i)~= str(y-j)) p=0; end j=j+1; end if(p==1 && j>=mid-1) disp('Palindrome'); else disp('Not a Palindrome'); end Problem(B) Program to check the entered number is palindrome or not clc; clear all; str=input('Enter the string ','s'); [x,y]=size(str); j=0; mid=y/2; p=1; %checking string from start and end for i=1:mid if (str(i)~= str(y-j)) p=0; end j=j+1; end if(p==1 && j>=mid-1) disp('Palindrome'); else disp('Not a Palindrome'); end Program to check the entered number is palindrome or not clc; clear all; str=input('Enter the string ','s'); [x,y]=size(str); j=0; mid=y/2; p=1; %checking string from start and end for i=1:mid if (str(i)~= str(y-j)) p=0; end j=j+1; end if(p==1 && j>=mid-1) disp('Palindrome'); else disp('Not a Palindrome'); endRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.