How can I make a sentence in Alphabetical order

1 回表示 (過去 30 日間)
Yifei Qiu
Yifei Qiu 2021 年 4 月 7 日
コメント済み: Yifei Qiu 2021 年 4 月 7 日
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
Johannes Hougaard 2021 年 4 月 7 日
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 件のコメント
Yifei Qiu
Yifei Qiu 2021 年 4 月 7 日
Thank you! It is really helpful!

サインインしてコメントする。

その他の回答 (1 件)

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by