How do I do this? I NEED HELP!!!

Create a MATLAB script that asks the user repeatedly for a string, and it verifies if the string has its characters ordered based on their numerical value. If the characters are ordered in ascending order then the output will be a message like “Your string is ordered” otherwise the message will be something like “Not in order!”. The program will stop when the user enters the character 0, otherwise it will keep asking for a new string. Create your own functions if needed. This is how the interactive input/output will look like:
Enter your string: "adhkp"
Your string "adhkp" has its characters ordered!
Enter your string: "3gtAq"
Your string "3gtAq" has its characters not ordered!
Enter your string: ";9Zalpq"
Your string ";9Zalpq" has its characters ordered!
. . .
. . .
Enter your string: "0"

5 件のコメント

per isakson
per isakson 2018 年 11 月 5 日
Homework? What have you done so far?
Andrew Anderson
Andrew Anderson 2018 年 11 月 5 日
Yes. I'm completely lost on how to answer my questions.
per isakson
per isakson 2018 年 11 月 5 日
編集済み: per isakson 2018 年 11 月 5 日
I guess you are better helped by doing MATLAB Onramp (free) while you are waiting for an answer here. It's good!
Do you find those exercises trivial?
Andrew Anderson
Andrew Anderson 2018 年 11 月 5 日
Yeah... there not super helpful. I appreciate having it as a future source. I'm in a rush and I'm really lost
per isakson
per isakson 2018 年 11 月 5 日
Ok, but did you find it trivial? If not it's difficult to help you doing your homework.

回答 (1 件)

Star Strider
Star Strider 2018 年 11 月 5 日

0 投票

You need to determine if the characters are sorted.
(For homework problems, we give hints.)

4 件のコメント

Andrew Anderson
Andrew Anderson 2018 年 11 月 5 日
I know I have to convert the characters to their corresponding numerical values, but how do I compare down the string to ensure the string is in order?
John D'Errico
John D'Errico 2018 年 11 月 5 日
THINK! Is each element greater than or equal to the previous one? If so, is it in order? So you just need a sequence of comparisons, comparing each to the one that preceded it.
per isakson
per isakson 2018 年 11 月 5 日
編集済み: per isakson 2018 年 11 月 5 日
@John, The text of the assignment is full of double quotes; the user is supposed to give the answer in double quotes, e.g. ";9Zalpq". Doesn't this mean that the task is supposed to be solved with strings? With strings I fail to find a clean way to compare elements. String is "high level" and Matlab provides issorted for the task, "ordered based on their numerical value", (with "numerical" replaced by "unicode").
Walter Roberson
Walter Roberson 2018 年 11 月 5 日
One of the fundamental properties of string scalars is that you can use {} indexing to get at the character vector. Which you could then process in various ways including diff()

この質問は閉じられています。

質問済み:

2018 年 11 月 5 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by