How to find length of user input?

2 Ansichten (letzte 30 Tage)
Ashwin Sivalingam
Ashwin Sivalingam am 26 Feb. 2019
Beantwortet: emad xa am 6 Apr. 2020
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?

Akzeptierte Antwort

Stephan
Stephan am 26 Feb. 2019
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 Kommentare
Ashwin Sivalingam
Ashwin Sivalingam am 26 Feb. 2019
Bearbeitet: Ashwin Sivalingam am 26 Feb. 2019
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 am 26 Feb. 2019
Bearbeitet: Stephan am 26 Feb. 2019
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

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

Kategorien

Mehr zu Language Fundamentals finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by