#include <stdio.h> #include <string.h> enum Password_Strength { TOO_WEAK, STRONG_ENOUGH }; enum Password_Strength is_secure_password(char* password); int main(void) { char test_password[32]; do { printf("Choose a password: \n"); scanf("%31s", test_password); } while (is_secure_password(test_password) == TOO_WEAK); printf("%s is secure enough!\n", test_password); return 0; } int is_upper(char ch) { return (ch >= 'A' && ch <= 'Z'); } int is_lower(char ch) { return (ch >= 'a' && ch <= 'z'); } int is_digit(char ch) { return (ch >= '0' && ch <= '9'); } enum Password_Strength is_secure_password(char* password) { int length = strlen(password); int upper_count = 0; int lower_count = 0; int digit_count = 0; if (length < 8 || length > 12) // Minimum length requirement { return TOO_WEAK; } for (int i = 0; i < length; i++) { if (is_upper(password[i])) upper_count++; else if (is_lower(password[i])) lower_count++; else if (is_digit(password[i])) digit_count++; } if (upper_count >= 2 && lower_count >= 2 && digit_count >= 1) { return STRONG_ENOUGH; } else { return TOO_WEAK; } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter