Challenge - 5 Problems
String Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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);
Attempts:
2 left
💡 Hint
Remember strfind returns starting indices of the substring occurrences.
✗ Incorrect
The substring 'an' appears twice in 'banana': starting at positions 2 and 4.
❓ Predict Output
intermediate2: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);
Attempts:
2 left
💡 Hint
contains is case sensitive by default.
✗ Incorrect
The substring 'world' (lowercase) is not found in 'Hello World' (capital W), so contains returns false (0).
❓ Predict Output
advanced2: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);
Attempts:
2 left
💡 Hint
Check the 'IgnoreCase' option in contains.
✗ Incorrect
With 'IgnoreCase' set to true, contains finds 'SCIENCE' in 'Data Science' ignoring case, so returns true (1).
❓ Predict Output
advanced2: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);
Attempts:
2 left
💡 Hint
Think about how strfind treats empty strings.
✗ Incorrect
strfind returns all indices from 1 to length(str)+1 when searching for an empty substring.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about what each function returns as output type.
✗ Incorrect
contains returns a logical value true or false if substring is found; strfind returns the starting indices of each occurrence.