0
0
MATLABdata~20 mins

String searching (contains, strfind) in MATLAB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of strfind with multiple occurrences
What is the output of this MATLAB code?
str = 'banana';
idx = strfind(str, 'an');
disp(idx);
MATLAB
str = 'banana';
idx = strfind(str, 'an');
disp(idx);
A[1 3]
B[2 4]
C[2 3 4]
D[]
Attempts:
2 left
💡 Hint
Remember strfind returns starting indices of the substring occurrences.
Predict Output
intermediate
2:00remaining
Using contains to check substring presence
What does this MATLAB code output?
str = 'Hello World';
tf = contains(str, 'world');
disp(tf);
MATLAB
str = 'Hello World';
tf = contains(str, 'world');
disp(tf);
A1
B[]
C0
DError: case sensitivity
Attempts:
2 left
💡 Hint
contains is case sensitive by default.
Predict Output
advanced
2:00remaining
Case-insensitive contains usage
What is the output of this MATLAB code?
str = 'Data Science';
tf = contains(str, 'SCIENCE', 'IgnoreCase', true);
disp(tf);
MATLAB
str = 'Data Science';
tf = contains(str, 'SCIENCE', 'IgnoreCase', true);
disp(tf);
A0
B[]
CError: Invalid parameter
D1
Attempts:
2 left
💡 Hint
Check the 'IgnoreCase' option in contains.
Predict Output
advanced
2:00remaining
strfind with empty substring
What does this MATLAB code output?
str = 'example';
idx = strfind(str, '');
disp(idx);
MATLAB
str = 'example';
idx = strfind(str, '');
disp(idx);
A[1 2 3 4 5 6 7 8]
B[]
C[1]
DError: Empty substring not allowed
Attempts:
2 left
💡 Hint
Think about how strfind treats empty strings.
🧠 Conceptual
expert
2:00remaining
Difference between contains and strfind outputs
Which statement correctly describes the difference between MATLAB's contains and strfind functions when searching for a substring?
Acontains returns logical true/false indicating presence; strfind returns indices of occurrences.
Bcontains returns indices of occurrences; strfind returns logical true/false.
CBoth return indices but contains is case-insensitive by default.
DBoth return logical true/false but strfind is faster.
Attempts:
2 left
💡 Hint
Think about what each function returns as output type.