How to find length of user input?

15 ビュー (過去 30 日間)
Ashwin Sivalingam
Ashwin Sivalingam 2019 年 2 月 26 日
回答済み: emad xa 2020 年 4 月 6 日
Hi everyone. I have a program here to determine ISBN numbers of a given 9 digit number. However, I am running into some difficulty.
clc
clear
isbn = input('Enter a nine digit ISBN number:' );
if length(isbn) == 9
d1 = str2num(isbn(1));
d2 = str2num(isbn(2));
d3 = str2num(isbn(3));
d4 = str2num(isbn(4));
d5 = str2num(isbn(5));
d6 = str2num(isbn(6));
d7 = str2num(isbn(7));
d8 = str2num(isbn(8));
d9 = str2num(isbn(9));
A = (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9);
mod (A, 11);
d10 = mod;
if d10 ~= 10
fprintf ("ISBN-10: %f-%f%f-%f%f%f%f%f%f-%f",d1, d2, d3, d4, d5, d6, d7, d8, d9, d10);
elseif d10 == 10
d10 = X;
fprintf ("ISBN-10: %f-%f%f-%f%f%f%f%f%f-%f",d1, d2, d3, d4, d5, d6, d7, d8, d9, X);
end
else if length(isbn) ~= 9
disp ('Error, ISBN must be exactly 9 digits.')
end
end
I am trying to get the length of the user input, but everytime I type in a 9 digit number, it reads me the Error display I wrote. It is not realizing the length value of "123456789" as 9 but rather as 1. Any suggestions?

採用された回答

Stephan
Stephan 2019 年 2 月 26 日
Hi,
a lots of bugs in this - i think i fixed them all:
clc
clear
isbn = input('Enter a nine digit ISBN number:' );
isbn = char(string(isbn));
if strlength(string(isbn)) == 9
d1 = str2double(isbn(1));
d2 = str2double(isbn(2));
d3 = str2double(isbn(3));
d4 = str2double(isbn(4));
d5 = str2double(isbn(5));
d6 = str2double(isbn(6));
d7 = str2double(isbn(7));
d8 = str2double(isbn(8));
d9 = str2double(isbn(9));
A = (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9);
d10 = mod (A, 11);
if d10 ~= 10
fprintf ("ISBN-10: %d-%d%d-%d%d%d%d%d%d-%d\n",d1, d2, d3, d4, d5, d6, d7, d8, d9, d10);
elseif d10 == 10
d10 = 'X';
fprintf ("ISBN-10: %d-%d%d-%d%d%d%d%d%d-%s\n",d1, d2, d3, d4, d5, d6, d7, d8, d9, d10);
end
else
if length(isbn) ~= 9
disp ('Error, ISBN must be exactly 9 digits.')
end
end
Best regards
Stephan
  2 件のコメント
Ashwin Sivalingam
Ashwin Sivalingam 2019 年 2 月 26 日
編集済み: Ashwin Sivalingam 2019 年 2 月 26 日
Thank you very much Stephan, this works. However, when entering in a value that begins with 0, for ex, 012345678, it ignores the 0 and counts 8 digits. Any fix?
Stephan
Stephan 2019 年 2 月 26 日
編集済み: Stephan 2019 年 2 月 26 日
clc
clear
isbn = input('Enter a nine digit ISBN number:', 's');
if strlength(isbn) == 9
d1 = str2double(isbn(1));
d2 = str2double(isbn(2));
d3 = str2double(isbn(3));
d4 = str2double(isbn(4));
d5 = str2double(isbn(5));
d6 = str2double(isbn(6));
d7 = str2double(isbn(7));
d8 = str2double(isbn(8));
d9 = str2double(isbn(9));
A = (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9);
d10 = mod (A, 11);
if d10 ~= 10
fprintf ("ISBN-10: %d-%d%d-%d%d%d%d%d%d-%d\n",d1, d2, d3, d4, d5, d6, d7, d8, d9, d10);
elseif d10 == 10
d10 = 'X';
fprintf ("ISBN-10: %d-%d%d-%d%d%d%d%d%d-%s\n",d1, d2, d3, d4, d5, d6, d7, d8, d9, d10);
end
else
if length(isbn) ~= 9
disp ('Error, ISBN must be exactly 9 digits.')
end
end
should work

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

その他の回答 (1 件)

emad xa
emad xa 2020 年 4 月 6 日
i run program have a problem
Undefined function or variable 'strlength'.
Error in Q3 (line 4)
if strlength(isbn) == 9

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by