I need to create a function that sorts two strings into alphabetical order. any help?

function less=isLess(worda,wordb)
%input:
%worda, wordb = two strings
%output:
%less = true if worda is before wordb. False otherwise
end

Answers (1)

Based on your sudo code the following function works
function less = isLess(worda,wordb)
input = {worda, wordb};
inputSorted = sort(input);
if strcmp(input{1},inputSorted{1}) == true
less = true;
else
less = false;
end
end
Otherwise you can just use the first two lines to sort alphabetically.
input = {worda, wordb};
inputSorted = sort(input);

7 Comments

The assignment actually said that i could not use a built in sorting function or a function that could find minimum values. Could you revise it to fit these conditions?
Please refer to the following link regarding assignments on MATLAB Answers. I would be happy to help you out further but you need to show some of your own work first.
I have been working on it more. This is what i have
function sortedarray=sortWordsUpd(array)
sortedarray={};
while ~isempty(array)
[m,pos]=findSmallest(array);
array(pos)=[];
sortedarray=[sortedarray,m];
end
function [m,pos]=findSmallest(array)
m =array{1};
pos=1;
for i=1:(length(array))
if isLessWord(array{i},m)
m=array{i};
pos=i;
end
end
function less=isLessWord(wordA,wordB)
worda=lower(wordA);
wordb=lower(wordB);
for i=1:length(worda)
for j=1:length(wordb)
if worda(i)~=wordb(j)
less=worda(i)<wordb(j);
elseif worda(i)==wordb(j)
less=length(worda)<length(wordb);
end
end
end
>> sortWordsUpd({'Hello','hell','abc','aa','aza','aab','AaBb','a'})
ans =
1×8 cell array
Columns 1 through 7
{'a'} {'aa'} {'aza'} {'aab'} {'AaBb'} {'abc'} {'hell'}
Column 8
{'Hello'}
It is almost correct, 'aza' is just in the wrong spot
You are close. May I suggest using ASCII? I used a very similar method to yours it is just easier to work with numbers then letters. I make the following assumption that there are no spaces and all words are input as lowercase (you can easily add failsafes at the start of the function to make the string meet these requirments).
function out = alphabeticalOrder(worda,wordb)
% Convert to ASCII
wordA = double(worda);
wordB = double(wordb);
% Find the smaller word length as min function cant be used
if length(wordA) <= length(wordB)
k = 1;
else
k = 0;
end
% Compare each letter ASCII value to find if sorted correctly
for ii = 1:(length(wordA)*(k) + length(wordB)*(1-k))
% If the same letter go to next ii value
if wordA(ii) == wordB(ii)
continue
end
% If letter of worda before wordb true, otherwise false. Smaller number corresponds to start of alphabet.
if wordA(ii) > wordB(ii)
out = false;
return
else
out = true;
return
end
end
% Do a final check based off word length
if length(wordA) > length(wordB)
out = false;
return
end
out = true;
end
We have not learned the ii function in class so I am not able to use it, is ther another way? Thanks for the help as well.
ii is not a function it is just aa variable being used as a for loop index
I input the this part and got this as the answer:
sortWordsUpd({'Hello','hell','abc','aa','aza','aab','AaBb','a'})
ans =
1×8 cell array
Columns 1 through 7
{'AaBb'} {'Hello'} {'a'} {'aa'} {'aab'} {'abc'} {'aza'}
Column 8
{'hell'}

Sign in to comment.

Categories

Asked:

on 13 Nov 2018

Commented:

on 14 Nov 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!