Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use Matlab to write the code. An anagram is a word or phrase formed by re

ID: 3783541 • Letter: P

Question

Please use Matlab to write the code.

An anagram is a word or phrase formed by rearranging the letters of another word or phrase. Examples include: listen-silent cinema - ice man a telescope - to see place Spaces obviously don't count when determining if two sets of words are anagrams. Case doesn't matter. Write a function called anagram, m that compares two strings and returns 1 (logical true) if they are anagrams, and 0 (logical false) if they are not. You can use any built-in functions you like (and no, there isn't a built-in anagram function.) Examples: >> r = anagram('dormitory', 'dirty room') r= 1 >> r = anagram('yes', 'no') r = anagram ('dog', ' cat') r = anagram('conversation', 'conservation') r

Explanation / Answer

% matlab code

function f = anagram(str1, str2)
f = 1;
array = [];
for i=1:256
array(i) = 0;
end
% increment value at character index in array
for i=1:length(str1)
array(str1(i)) = array(str1(i)) + 1;
end
  
for i=1:length(str2)
if str2(i) != ' '
if ( array(str2(i)) > 0 )
array(str2(i)) = array(str2(i)) - 1;
else
f = 0;
end
end
end
end

disp(anagram('dormitory','dirty room'));
%output: 1
disp(anagram('yes','no'));
%output: 0
disp(anagram('dog','cat'));
%output: 0
disp(anagram('conversation','conservation'));
%output: 1