Write a function sortWordsarray that does the following Take

Write a function sortWords(array) that does the following: Takes as an input a cell array of strings consisting of letters only. Returns a cell array with all the strings in alphabetical order. The function should ignore whether letters are lower case or upper case. Test your function with the following: >> sorted = sortWords ({\'Hello\', \'hell\', \'abc\', \'aa\', \'aza\', \'aab\', \'AaBb\', \'a\'}) sorted = \'a\' \'a\' \'aab\' \'AaBb\' \'abc\' \'aza\' \'hall\' \'Hello\'

Solution

The test case given has passed and other test cases passed too.

Program
function sortedArray = sortWords(array)
sortedArray = {};
while ~isempty(array)
[m,pos] = findSmallest(array);
array(pos) = [];
   sortedArray = [sortedArray m];
end
end

function [m,pos] = findSmallest(array)
m = array{1,1};
pos = 1;
for i=2:length(array)
if(isLessWord(array{1,i},m) == 1)
       pos = i;
    m = array{1,i};
   end
end
end

function less = isLessWord(wordA,wordB)
worda=lower(wordA);
wordb=lower(wordB);
len1=numel(worda);
len2=numel(wordb);
small=worda;
big=wordb;
smallReturn=wordA;
bigReturn=wordB;
less = true;
if(len1>len2)
   small=wordb;
   big=worda;
smallReturn=wordB;
bigReturn=wordA;
less = false;
end
for i=1:numel(small)
   if(small(i)<big(i))
       break;
   elseif (big(i)<small(i))
       less = ~less;
       break;
else
    continue;
   end
end
end

 Write a function sortWords(array) that does the following: Takes as an input a cell array of strings consisting of letters only. Returns a cell array with all

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site