How can I make a sentence in Alphabetical order

5 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2021-4-7
编辑:Stephen23 2021-4-7
Is the goal of this homework to implement a sorting algorithm?
Yifei Qiu
Yifei Qiu 2021-4-7
Yes, and it is not allowed to use the built-in function like "strrep","replaceBetween","regexprep", "return" or other similar functions.

请先登录,再进行评论。

采纳的回答

Johannes Hougaard
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 个)

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

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by