0
0
MATLABdata~10 mins

String comparison 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 compare two strings for equality.

MATLAB
result = strcmp(str1, [1]);
Drag options to blanks, or click blank then click option'
Astr1
Bstr2
Cstring2
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same variable twice like strcmp(str1, str1) which always returns true.
Using a variable name that does not exist.
2fill in blank
medium

Complete the code to check if the variable inputStr matches the word 'hello'.

MATLAB
isMatch = strcmp([1], 'hello');
Drag options to blanks, or click blank then click option'
AinputStr
Btext
Cgreeting
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string literal instead of the variable.
Mixing up the order of arguments (though strcmp is symmetric).
3fill in blank
hard

Fix the error in the code to correctly compare two strings a and b.

MATLAB
if [1](a, b)
    disp('Strings are equal');
else
    disp('Strings are different');
end
Drag options to blanks, or click blank then click option'
Astrcmp
Bstrcmps
Cisequal
Dstrcmpi
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent function like strcmps.
Using strcmpi when case sensitivity matters.
4fill in blank
hard

Fill both blanks to create a dictionary (containers.Map) that maps words to their lengths, but only include words longer than 3 characters.

MATLAB
words = {'apple', 'cat', 'banana', 'dog'};
lengths = containers.Map([1], [2]);

function keys = getKeys(words)
    keys = {};
    for i = 1:length(words)
        if length(words{i}) > 3
            keys{end+1} = words{i};
        end
    end
end

function values = getValues(words)
    values = [];
    for i = 1:length(words)
        if length(words{i}) > 3
            values(end+1) = length(words{i});
        end
    end
end

keys = getKeys(words);
values = getValues(words);
Drag options to blanks, or click blank then click option'
Akeys
Bwords
Cvalues
Dlengths
Attempts:
3 left
💡 Hint
Common Mistakes
Using the original words list without filtering.
Swapping keys and values in the constructor.
5fill in blank
hard

Fill all three blanks to create a logical array that marks which strings in listStr start with the letter 'a' (case insensitive).

MATLAB
listStr = {'apple', 'Banana', 'avocado', 'Cherry'};
startsWithA = arrayfun(@(x) [1](x, '[3]', [2]), listStr);
Drag options to blanks, or click blank then click option'
Astrncmpi
B1
Ca
Dstrcmp
Attempts:
3 left
💡 Hint
Common Mistakes
Using strcmp which compares whole strings.
Using strncmp which is case sensitive.