# write a code to sort string

8 ビュー (過去 30 日間)
Aminata camara 2021 年 3 月 17 日
コメント済み: Aminata camara 2021 年 3 月 18 日
how to I write a code for this problem:
Write a program that accepts a string from a user with the input function, chops that string into a series of tokens, sorts the tokens into ascending order, and prints them out.
this is what I wrote so far I dont know what to do
%Input a user string
str=input('Enter string:','s');
[token,remain]=strtok(str,',')
##### 2 件のコメントなしを表示なしを非表示
ANKUR KUMAR 2021 年 3 月 17 日
Could you please elaborate "chops that string into a series of tokens"? Where comes the token in the string?
Aminata camara 2021 年 3 月 17 日
that what the problem is saying, I understand it like when I'm writting a series of world
example if a user enter : anna,mimi,jen
the user will get
'anna'
'mimi'
'jen'
also I dont get what the problem is saying by then sort that information.

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

### 回答 (1 件)

ANKUR KUMAR 2021 年 3 月 17 日
str=input('Enter string:','s');
strsplit(str,',')
On which basis you wish to sort strings? Is it alphabatically, or based on the length ?
##### 9 件のコメント7 件の古いコメントを表示7 件の古いコメントを非表示
Walter Roberson 2021 年 3 月 18 日
B = [8, -2, 11]
B = 1×3
8 -2 11
[sortedB, idx] = sort(B)
sortedB = 1×3
-2 8 11
idx = 1×3
2 1 3
So the first output of sort(B) is the list of sorted values (by default in ascending order.)
The second output of sort() tells you where each of the outputs came from in the original vector. So where the first entry, idx(1) is 2, that tells you that B(2) was the value that sorted into first place. The second entry, idx(2) is 1, telling you that B(1) was the entry that sorted second. The third entry, idx(3) is 3, telling you that B(3) is the entry in B that sorted third.
When you have that kind of order information, you can use it to index another array, and the result is reordering the other array to have order corresponding (increasing) B.
The syntax
[~, idx] = sort(B)
means the same as
[AnInternalVariableNameIsHere, idx] = sort(B);
clear AnInternalVariableNameIsHere
That is, the output is generated, but it is thrown away immediately. Using ~ is a short way to ignore an output when you need to access a later output.
Aminata camara 2021 年 3 月 18 日
ooh I see now thank you for the great explanation

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

### カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by