How can I make a sentence in Alphabetical order

조회 수: 5 (최근 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개)

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 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