Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "password_checker.h"
- /* Declare conditions */
- char upperCases[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
- 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
- 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
- char lowerCases[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
- 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
- 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
- char specials[] = {'`', '!', '\"', '?', '$', '?', '%', '^', '&',
- '*', '(', ')', '_', '-', '+', '=', '{', '[',
- '}', ']', ':', ';', '@', '\'', '~', '#', '|',
- '\\', '<', '>', '.', '?'};
- char numbers[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
- /*
- * Function: CheckPasswordHealth
- * ----------------------------
- * Check password health.
- * pPassword: Password string.
- * returns: Return -1: password length is smaller than 10 characters.
- * Return 0: password contains invalid characters.
- * Return 1 or return 2: password health is low.
- * Return 3: password health is medium.
- * Return 4: password health is strong.
- */
- int CheckPasswordHealth(char *pPassword) {
- int pLength = strlen(pPassword);
- if (pLength < 10)
- return -1;
- else
- if (pLength > 20)
- return 5;
- else {
- int i, Count = 0;
- char *checkUp, *checkLow, *checkSpecial, *checkNum;
- for (i = 0; i < pLength; i++) {
- checkUp = strchr(upperCases, pPassword[i]);
- if (checkUp != NULL) {
- Count++;
- //break;
- }
- }
- for (i = 0; i < pLength; i++) {
- checkLow = strchr(upperCases, pPassword[i]);
- if (checkLow != NULL) {
- Count++;
- //break;
- }
- }
- for (i = 0; i < pLength; i++) {
- checkSpecial = strchr(upperCases, pPassword[i]);
- if (checkSpecial != NULL) {
- Count++;
- //break;
- }
- }
- for (i = 0; i < pLength; i++) {
- checkNum = strchr(upperCases, pPassword[i]);
- if (checkNum != NULL) {
- Count++;
- //break;
- }
- }
- return Count;
- }
- }
- int main() {
- printf("========== Password health checker ==========\n");
- printf("This program is to check password health with 3 levels: ");
- printf("low, medium and strong.\nEnter password ");
- printf("(password must be no smaller than 10 and no larger than 20): ");
- char psswd[51];
- scanf("%50[^\n]s", psswd);
- int checker = CheckPasswordHealth(psswd);
- if (checker == -1)
- printf("Your password length is smaller than 10 characters.");
- else
- if (checker == 0)
- printf("Your password contain invalid characters.");
- else
- if (checker == 1 || checker == 2)
- printf("Your password health is low.");
- else
- if (checker == 3)
- printf("Your password health is medium.");
- else
- if (checker == 4)
- printf("You password health is strong.");
- else
- if (checker == 5)
- printf("Your password length is larger than 20 character.");
- }
Advertisement
Add Comment
Please, Sign In to add comment