Effacer les filtres
Effacer les filtres

How can I make a sentence in Alphabetical order

5 vues (au cours des 30 derniers jours)
Yifei Qiu
Yifei Qiu le 7 Avr 2021
Commenté : Yifei Qiu le 7 Avr 2021
How can I make the sentence 'the quick brown fox' in alphabetical order
Here is my code, but there are some problem with it. It does not give the answer as I expected.
function[output] = sentenceSort(x)
%%%The input x is a string array which is not in alphabetically
%%%order
%%%The output is the a string array which is in alphabetically order
ind=find(x==' ');%find the position of the space
num=size(ind,2)+1; %determine the number of words in a sentence
count = 1; %set up a variable
words = strings(); %set up an empty string array
for i = 1:num
if i ~= num
words(i)= x(count:ind(i)-1); %fill out each word to corresponding position
count = ind(i)+1;
elseif i== num
words(num) = x(count:length(x));
end
end
for i = 1:num-1
for j = 1:num-1
curr = words(i);
next = words(j+1);
if words(i)<words(j)
output = words
elseif curr > next
words(j) = next;
words(j+1) = curr;
output = words
end
end
end
  2 commentaires
Stephen23
Stephen23 le 7 Avr 2021
Modifié(e) : Stephen23 le 7 Avr 2021
Is the goal of this homework to implement a sorting algorithm?
Yifei Qiu
Yifei Qiu le 7 Avr 2021
Yes, and it is not allowed to use the built-in function like "strrep","replaceBetween","regexprep", "return" or other similar functions.

Connectez-vous pour commenter.

Réponse acceptée

Johannes Hougaard
Johannes Hougaard le 7 Avr 2021
I think a minor modification of your function like this
function[output] = sentenceSort(x)
%%%The input x is a string array or a character array which is not in alphabetically
%%%order
%%%The output is the a string array which is in alphabetically order
if isstring(x) % convert a string to a character array for the find spaces to work
x = char(x);
end
ind=find(x==' ');%find the position of the space
num=size(ind,2)+1; %determine the number of words in a sentence
count = 1; %set up a variable
words = strings(); %set up an empty string array
for i = 1:num
if i ~= num
words(i)= x(count:ind(i)-1); %fill out each word to corresponding position
count = ind(i)+1;
elseif i== num
words(num) = x(count:length(x));
end
end
output = strings(size(words));
for ii = 1:num
wordpos = sum(words < words(ii))+1;
if output(wordpos) == ""
output(wordpos) = words(ii);
else % If two words are identical find the position for the latter
wordpos = find(output == words(ii),1,'last') + 1;
output(wordpos) = words(ii);
end
end
would do the trick without using internal string functions.
If the use of 'sort' is allowed it could simply be sorted by
function[output] = sentenceSort(x)
%%%The input x is a string array or a character array which is not in alphabetically
%%%order
%%%The output is the a string array which is in alphabetically order
if isstring(x) % convert a string to a character array for the find spaces to work
x = char(x);
end
ind=find(x==' ');%find the position of the space
num=size(ind,2)+1; %determine the number of words in a sentence
count = 1; %set up a variable
words = strings(); %set up an empty string array
for i = 1:num
if i ~= num
words(i)= x(count:ind(i)-1); %fill out each word to corresponding position
count = ind(i)+1;
elseif i== num
words(num) = x(count:length(x));
end
end
output = sort(words);
  1 commentaire
Yifei Qiu
Yifei Qiu le 7 Avr 2021
Thank you! It is really helpful!

Connectez-vous pour commenter.

Plus de réponses (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov le 7 Avr 2021
Hi,
Here is an easy solution to your exercise:
CC='the quick brown fox';
ANS =sort(strsplit(strtrim(CC)))
Good luck.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by