Complete the code to compare two strings for equality.
result = strcmp(str1, [1]);The strcmp function compares two strings and returns true if they are exactly the same. Here, str2 is the second string to compare with str1.
Complete the code to check if the variable inputStr matches the word 'hello'.
isMatch = strcmp([1], 'hello');
The strcmp function compares inputStr to the string 'hello'. If they are the same, isMatch will be true.
Fix the error in the code to correctly compare two strings a and b.
if [1](a, b) disp('Strings are equal'); else disp('Strings are different'); end
The correct function to compare two strings for exact equality in MATLAB is strcmp. The option strcmpi ignores case, isequal works for arrays but is not recommended for strings, and strcmps is not a valid function.
Fill both blanks to create a dictionary (containers.Map) that maps words to their lengths, but only include words longer than 3 characters.
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);The containers.Map constructor takes two cell arrays: one for keys and one for values. Here, keys holds the filtered words, and values holds their lengths.
Fill all three blanks to create a logical array that marks which strings in listStr start with the letter 'a' (case insensitive).
listStr = {'apple', 'Banana', 'avocado', 'Cherry'};
startsWithA = arrayfun(@(x) [1](x, '[3]', [2]), listStr);strcmp which compares whole strings.strncmp which is case sensitive.The function strncmpi compares the first N characters of two strings ignoring case. Here, N is 1, and the character to compare is 'a'.