How to check whether a given password is strong, medium, or weak in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
#include <ctype.h>

const char* checkPasswordStrength(const char* password) {
    int length = strlen(password);
    int hasLower = 0, hasUpper = 0;
    int hasDigit = 0, specialChar = 0;

    const char* lowuppdig = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

    for (int i = 0; i < length; i++) {
        if (islower(password[i]))
            hasLower = 1;
        if (isupper(password[i]))
            hasUpper = 1;
        if (isdigit(password[i]))
            hasDigit = 1;

        if (strchr(lowuppdig, password[i]) == NULL) {
            specialChar = 1;
        }
    }

    if (hasLower && hasUpper && hasDigit && specialChar && length >= 10)
        return "Strong";
    else if ((hasLower || hasUpper) && specialChar && length >= 8)
        return "Medium";

    return "Weak";
}

int main() {
    const char* password = "aq1o@p9$XM";
    
    printf("%s\n", checkPasswordStrength(password));
    printf("%s\n", checkPasswordStrength("asW!W)(o"));
    printf("%s\n", checkPasswordStrength("WSDFK!#Q"));
    printf("%s\n", checkPasswordStrength("n*djskq*"));
    printf("%s\n", checkPasswordStrength("WE3q#$"));

    return 0;
}


   
/*
run:
   
Strong
Medium
Medium
Medium
Weak

*/
 

 



answered Oct 22, 2024 by avibootz
edited Oct 22, 2024 by avibootz
...