0
0
MATLABdata~10 mins

String searching (contains, strfind) in MATLAB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the string 'hello' contains the substring 'll'.

MATLAB
result = [1]('hello', 'll');
Drag options to blanks, or click blank then click option'
Aindex
Bstrfind
Cfindstr
Dcontains
Attempts:
3 left
💡 Hint
Common Mistakes
Using strfind returns indices, not a logical value.
2fill in blank
medium

Complete the code to find the starting index of substring 'cat' in the string 'concatenate'.

MATLAB
index = [1]('concatenate', 'cat');
Drag options to blanks, or click blank then click option'
Acontains
Bstrfind
Cfindstr
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using contains returns logical, not index.
3fill in blank
hard

Fix the error in the code to correctly find if 'dog' is in 'hotdog'.

MATLAB
found = [1]('hotdog', 'dog');
if found
    disp('Found!')
end
Drag options to blanks, or click blank then click option'
Afindstr
Bstrfind
Ccontains
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using strfind returns indices, not a logical value.
4fill in blank
hard

Fill both blanks to create a dictionary of words and their starting indices if the word length is greater than 3.

MATLAB
words = {'apple', 'bat', 'cat', 'doge'};
indices = containers.Map();
for i = 1:length(words)
    word = words{i};
    if length(word) [1] 3
        indices(word) = [2]('concatenate', word);
    end
end
Drag options to blanks, or click blank then click option'
A>
Bstrfind
C<
Dcontains
Attempts:
3 left
💡 Hint
Common Mistakes
Using contains instead of strfind for index.
5fill in blank
hard

Fill all three blanks to create a map of uppercase words and their indices if the word contains 'at'.

MATLAB
words = {'cat', 'dog', 'bat', 'rat'};
indices = containers.Map();
for i = 1:length(words)
    word = words{i};
    if [1](word, 'at')
        indices([2]) = [3]('concatenate', word);
    end
end
Drag options to blanks, or click blank then click option'
Acontains
Bword
Cstrfind
Dupper(word)
Attempts:
3 left
💡 Hint
Common Mistakes
Using word instead of upper(word) as key.