Effacer les filtres
Effacer les filtres

How to find length of user input?

3 vues (au cours des 30 derniers jours)
Ashwin Sivalingam
Ashwin Sivalingam le 26 Fév 2019
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?

Réponse acceptée

Stephan
Stephan le 26 Fév 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 commentaires
Ashwin Sivalingam
Ashwin Sivalingam le 26 Fév 2019
Modifié(e) : Ashwin Sivalingam le 26 Fév 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 le 26 Fév 2019
Modifié(e) : Stephan le 26 Fév 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

Connectez-vous pour commenter.

Plus de réponses (1)

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

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by