/* * check_password_rules.c */ #include #include #define MAX_PASSWORD_SIZE 256 /* Given a string, check whether it meets the quality rules for UMD Directory passwords Implement: * A password must be at least 8 and no more than 32 characters in length. * A password must contain at least one uppercase letter. * A password must contain at least one lowercase letter. * A password must contain at least one character from the set of digits or punctuation characters (such as # @ $ & among others). * A password may not begin or end with the space character. * A password may not contain more than two consecutive identical characters. Do not implement * A password may not be (or be a variation of) a dictionary word in English or many other languages. This includes making simple substitutions of digits or punctuation that resemble alphabetic characters (such as replacing the letter S in a common word with the $ symbol). * You may not reuse a password you have already used. */ int check_password_rules(char s[]) { int length = 0; // stores the password length int uppercase = 0; // count of uppercase characters int lowercase = 0; // count of lowercase characters int special = 0; // count of special characters int digit = 0; // count of digits int begin_space = 0; // the string begins with a space int end_space = 0; // the string ends with a space int consecutive = 0; // the string has consecutive identical chars char c, old_c; int i; i = 0; while (i < MAX_PASSWORD_SIZE && (c = s[i]) != '\0') { if (c >= 'A' && c <= 'Z') { // Could also use isupper() uppercase++; } else if (c >= 'a' && c <= 'z') { // Could also use islower() lowercase++; } else if (c >= '0' && c <= '9') { // Could also use isdigit() digit++; } else { // Could also use isspecial() switch (c) { // Count special characters case '#': case '@': case '$': case '&': // and so on special++; break; // Check space at the beginning case ' ': if (i==0) { begin_space = 1; } break; } } // if-else // Check consecutive characters if (i > 0 && old_c == c) { consecutive = 1; } old_c = c; i++; } // Check space at the end end_space = (old_c == ' '); // Update the length length = i; // Print debugging info printf ("length = %d\nuppercase = %d\nlowercase = %d\ndigit = %d\nspecial = %d\nbegin_space = %d\nend_space = %d\nconsecutive = %d\n", length, uppercase, lowercase, digit, special, begin_space, end_space, consecutive); return length >= 8 && length <= 32 && uppercase > 0 && lowercase > 0 && (digit > 0 || special > 0) && !begin_space && !end_space && !consecutive; } int main () { char password[MAX_PASSWORD_SIZE]; // Read password and check UMD rules printf("Enter your password: "); fgets(password, MAX_PASSWORD_SIZE, stdin); // Remove the final '\n' returned by fgets password[strlen(password) - 1] = '\0'; printf("The string '%s' %s the UMD password requirements", password, check_password_rules(password) ? "meets" : "does not meet"); return 0; }