C Programming Tutorial - 33 -Challenge #1!

+3 Dejan B · November 30, 2014
Hello to everyone!
I try and I think it works fine, but can you tell me some improvement?
Thanks!
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

int main()

{

    char password[21];
    int check = 0;
    int capital = 0, number = 0, dollar = 0;

    printf("This program will check the strength of your password.\nEnter a password with:\nat least 8 characters long,\nat least one capital letter,\nat least one number\nat least one $ sign:\n");
    scanf(" %s", &password);

    for(check=0; check < strlen(password); check++){

        if( isalpha(password[check]) ){
            if( isupper(password[check]) ){
            capital++;
            }
        }

        if( isdigit(password[check]) ){
            number++;
        }

        if( password[check] == '$' ){
            dollar++;
        }
      }

    if(capital >= 1){
        printf("\nCapital test: %d\tPassed\n", capital);
    }else{
        printf("\nCapital test: 0\tFailed\n");
        }

    if(number >= 1) {
        printf("Number test: %d\tPassed\n", number);
    }else{
        printf("Number test: 0\tFailed\n");
        }

    if(dollar >= 1){
        printf("Dollar test: %d\tPassed\n", dollar);
    }else{
        printf("Dollar test: 0\tFailed\n");
        }

    if(strlen(password) >= 8){
        printf("Minimum required length: %d\tPassed\n", strlen(password));
    }else{
        printf("Minimum required length: %d\tFailed\n", strlen(password));
        }

    if (capital == 0 || number == 0 || dollar == 0 || strlen(password) <=7){
        printf("\nYour password needs improving.\n");
    }else{
        printf("\nYour password is strong and have %d characters!\n", strlen(password));
    }
   return 0;
}




Post a Reply

Replies

- page 2
Oldest  Newest  Rating
+1 Jason Amador · December 1, 2014
You might discourage sequential characters, like abc or 123.  And even further would be to check against a dictionary and discourage words.
+1 Dejan B · November 30, 2014
Thanks for the advice, I edited the first post!
+2 Mathias Frits Rørvik · November 30, 2014
You could add a check for setting a minimum required length.
Example: the password has to be at least 8 characters long.
  • 1
  • 2

C

107,245 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator