I know that isdigit is used to check if char has digits. Is there a way to check if a string has a number in a similar way. Also for upper and lower cases?
I think you mean ...that one or more characters in a string is a digit.....or ... if a character is a digit. Yes the isdigit() function is from the Standard C Library <cctype> and takes no notice of case. It returns non zero if the character is a digit otherwise it returns 0.
Well I have been doing a program that checks the length of a string. Also if it has a upper or lower case and finally if it has any numerical value. I already have the length correct by doing an "if" statement saying
1 2
if(pass.length()>LENGTH){returntrue; elsereturnfalse}//In a funct.
//LENGTH is 6 and it is a "const int variable"
. But now i need something to check for numbers and the upper or lower cases. Any ideas?
Here is snippet of the program.
I could have used chars instead of strings to make life easier but I have to use strings.
int main()
{
bool ok = true;
// Explain program to user and request a password
cout << "This program checks passwords to see if they are secure.";
cout << "\nEnter a password to check: ";
// Read user's input
string password;
cin >> password;
// Check the password
if (!isLongEnough(password))
{
cout << "Password must be at least six characters long." << endl;
ok = false;exit(0);
}
if (!hasDigit(password))
{
cout << "Password must have at least one digit." << endl;
ok = false;
}
if (!hasUpperAndLowerCase(password))
{
cout << "Password must have both lower case and upper case letters." << endl;
ok = false;
}
if (ok) {
cout << "The password " << password << " is OK." << endl;
}
return 0;
}