I need to take a string ,and return the first recurring character.

1 回表示 (過去 30 日間)
Jose Hernandez
Jose Hernandez 2021 年 11 月 29 日
編集済み: Tyler F 2021 年 12 月 14 日
n this homework you must take a string and return the first recurring character in that string. For example if you are given ‘ABCA’ as the input then there is only one recurring character and your programming code should return ‘A’. If you are given ‘ABCBA’ then there is two recurring character ‘A’ and ‘B’ but the first recurring character is ‘B’ thus your code should return ‘B’. If you are given ‘ABC’ then there is no recurring character and your code returns ‘there is no recurring character’. You must use only commands and functions that we learned in this class during the semester.
  3 件のコメント
Stephen23
Stephen23 2021 年 12 月 1 日
編集済み: Stephen23 2021 年 12 月 1 日
Original question by Jose Hernandez retrieved from Google Cache:
I need to take a string ,and return the first recurring character.
n this homework you must take a string and return the first recurring character in that string. For example if you are given ‘ABCA’ as the input then there is only one recurring character and your programming code should return ‘A’. If you are given ‘ABCBA’ then there is two recurring character ‘A’ and ‘B’ but the first recurring character is ‘B’ thus your code should return ‘B’. If you are given ‘ABC’ then there is no recurring character and your code returns ‘there is no recurring character’. You must use only commands and functions that we learned in this class during the semester.
Rena Berman
Rena Berman 2021 年 12 月 13 日

(Answers Dev) Restored edit

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

回答 (2 件)

Murali Manikanti
Murali Manikanti 2021 年 12 月 1 日
Assuming that only A-Z characters are allowed in the input string. Scan the String from left to right and set the flag to true for each character visited. (say visited(‘A’) = true). At any index, if visited(‘char’) is already true then return that character.
You can make use of loops and arrays in MATLAB to convert the above logic to code.

Tyler F
Tyler F 2021 年 12 月 14 日
編集済み: Tyler F 2021 年 12 月 14 日
Without helping you too much for a HW assignment, there are a few other commands you can use that may be included in your approved command list. As Murali said you can loop over and keep a register of which characters you have visited. You could also use a loop and the any() command to check if any of the next characters are equal to the current character without having a predefined list of characters (e.g. word = 'ABCDEBF'; any(word(3:end) == word(2));.
You could also use the unique command to quickly tell if there were no repeated characters (ill let you read the documentation on your own for this one)

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by