/* * 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 = -1; while ((i+1) <= MAX_PASSWORD_SIZE && (c = s[++i]) != '\0') { length++; // Check character classes if (c >= 'A' && c <= 'Z') { uppercase++; } else if (c >= 'a' && c <= 'z') { lowercase++; } else if (c >= '0' && c <= '9') { digit++; } else { switch (c) { case '#': case '@': case '$': case '&': // Omit the rest, for brevity special++; break; case ' ': if (i==0) begin_space = 1; break; } } // Check consecutive characters if (i >= 1 && c == old_c) { consecutive = 1; } // Maintain old_c old_c = c; } end_space = (old_c == ' '); // Print debugging info // printf ("length = %d\nuppercase = %d\nlowercase = %d\nspecial = %d\nbegin_space = %d\nend_space = %d\nconsecutive = %d\n", // length, uppercase, lowercase, 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[strnlen (password, MAX_PASSWORD_SIZE) - 1] = '\0'; printf ("The string %s %s the UMD password requirements", password, check_password_rules(password) ? "meets" : "does not meet"); return 0; }