converting string code into numbers?
情報
この質問は閉じられています。 編集または回答するには再度開いてください。
古いコメントを表示
Write a function m-file called stringcode that takes a string as input and produces an array containing a code for each letter in the string with A = 0, B = 1, ..., Z = 25 and all other characters getting the value 26
Hints
- Convert the string to all upper case (using the command upper).
- Convert the string to an array of ascii codes (use double).
- Loop through the array and change the codes to the right values. Look up the codes for ’A’ and ’Z’.
here is my code
function num = stringcode( word )
asc = double(upper(word))
if asc >= 33 & asc <=47 & asc < 0
asc = 26
else asc = double(upper(word))
end
% simple arithmetic
num = asc - double('A')
end
I cant get the special characters to come up as the number 26
4 件のコメント
Walter Roberson
2020 年 1 月 26 日
Your input is a vector. asc is a vector. Your if is testing a vector. When you test a vector in if then it is only true if every value is true (non-zero).
Also there are no values that are both in the range 33 to 47 and less than 0 simultaneously.
Mohammad Sami
2020 年 1 月 28 日
編集済み: Mohammad Sami
2020 年 1 月 28 日
Why don't you do this
num = double(upper(asc)- 'A') ;
num(num <0 || num > 25) = 26;
Walter Roberson
2020 年 1 月 28 日
The homework requires that a loop be used.
Rik
2020 年 1 月 28 日
Also, you need to use a single pipe character for an array, so that second line doesn't work.
回答 (0 件)
この質問は閉じられています。
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!