How can I make a sentence in Alphabetical order
1 回表示 (過去 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 件のコメント
採用された回答
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
2021 年 4 月 7 日
Hi,
Here is an easy solution to your exercise:
CC='the quick brown fox';
ANS =sort(strsplit(strtrim(CC)))
Good luck.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!