Ensuring that a number is present in a string?
Ältere Kommentare anzeigen
I am writing a code that asks a user for a password, and it must satisfy the following conditions:
- at least 8 characters in length,
- have at least one capital letter, and one lowercase
- include a number
I have the top two figured out but I don't know how to write a function for the third. It is all in a "while" loop and a value of 1 is assigned to each condition... When all 3 are met, the "while" loop stops repeating.
How can I write the function that will determine if there is a number in the password the user inputs?
Thanks for the help!
1 Kommentar
Akzeptierte Antwort
Weitere Antworten (1)
Walter Roberson
am 10 Apr. 2014
1 Stimme
all(lower(password)==password) implies no capital letters. It does not, however, establish that any lowercase letters were used: the password might contain no alphabetic letters at all.
any(password >= '0' & password <= '9') is one of the ways to check for digits.
You should be considering using regexp() instead of looping.
2 Kommentare
Walter Roberson
am 10 Apr. 2014
Bearbeitet: Andrew Newell
am 10 Apr. 2014
~any(cellfun(@isempty, regexp(str, {'.{8,}', '[A-Z]', '[a-z]', '\d'}, 'match', 'once')))
Andrew Newell
am 10 Apr. 2014
Bearbeitet: Andrew Newell
am 10 Apr. 2014
Nice!
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!