Working Password strength checker

+3 Bernhard Riemann Georg Friedrich · July 2, 2015
#include 
#include
#include
#include
#include


int main(){

    char password[32];

    int hasUpper = 0;
    int hasLower = 0;
    int hasSymbol = 0;
    int hasNumber = 0;
    //int isMoreThanSevenLong = 0;
    int i;

    do{
        printf("Enter your password, it has to contain at least 8 letters, a number, an upper case, a lower case and a special character:");
        scanf("%s", password);
        /*if(sizeof(password) > 7 ){
            isMoreThanSevenLong = 1;
        }*/
        for(i = 0; i < 33; i++){ //33 because of string terminator
            int character = password[i];

            if(isupper(character)){
                if(hasUpper == 0){
                    hasUpper = 1;
                    continue;
                }
            } else if(islower(character)){
                if(hasLower == 0){
                    hasLower = 1;
                    continue;
                }
            } else if(isdigit(character)){
                if(hasNumber == 0){
                    hasNumber = 1;
                    continue;
                }
            } else{
                if(hasSymbol == 0){
                    hasSymbol = 1;
                    continue;
                }
            }
        }

    } while((hasUpper && hasLower && hasSymbol /*&& isMoreThanSevenLong*/ && hasNumber) == 0);

    printf("Password is %s", password);

    //printf("%d", sizeof(password));


    return 0;
}



You don't have to read it, it just satisfies to post a working program.

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
+1 Aju Antony · July 2, 2015
awesome job bro :D
0 c student · July 3, 2015
if(sizeof(password) > 7 )

checks if the size of the password variable is greater than 7 and since you've defined it as having a size of 32, it will always be true.
0 Bernhard Riemann Georg Friedrich · July 6, 2015
Oh yeah. I was gonna make that changeable for the user, but noticed that it's useless, so I forgot that code there. Thanks for pointing it out. Would you know what the default value of a character in a character array is?
0 c student · July 6, 2015
default value for a character?  i'm not sure what you mean
0 peymans johans · July 6, 2015
http://www.gowebsite.ir
0 Tobi Okubanjo · July 6, 2015
I'm not sure how this is actually checking the password all this is doing is printing out the password.
I had the same idea as you did the moment bucky told me about the challenge but mine doesn't seem to be functioning properly can you take a look at it and help me out. 


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int main()
{
    char Password[30];

    int hasUpper =0;
    int hasLower =0;
    int hasSymbol =0;
    int hasNumber =0;

        do{
            printf("Enter a password with a number, Uppper and lower case letters and a $ symbol.Make sure the number comes first.");
            scanf(" %s", &Password);

            if(isupper(Password)){
                if(hasUpper==0){
                    hasUpper = 1;
                    continue;
                }
            }else if(islower(Password)){
                if(hasLower==0){
                    hasLower=1;
                    continue;
                }
            }else if(isdigit(Password)){
                if(hasNumber==0){
                    hasNumber=1;
                    continue;
                }
            }else{
                if(hasSymbol==0){
                    hasSymbol=1;
                    continue;
                }
            }
        }while(hasLower && hasNumber && hasSymbol && hasUpper ==0);

        printf("Your password is %s", Password);
    return 0;
}
0 Bernhard Riemann Georg Friedrich · July 7, 2015

default value for a character? i'm not sure what you mean




When you haven't assigned a value to something:

char word[10] = "ham";


what is the value of word[3] - word[8]?
0 Bernhard Riemann Georg Friedrich · July 7, 2015
And I found your problem Tobi Okubanjo,
in the 5th line from the bottom you didn't add brackets around the logic tests:

}while((hasLower && hasNumber && hasSymbol && hasUpper) ==0);
0 c student · July 7, 2015
it depends.  it might contain random garbage from whatever else used it before your program or it might be initialized to 0.  don't assume anything, it's safer to initialize it yourself to be sure.
0 Bernhard Riemann Georg Friedrich · July 7, 2015
thanks
  • 1
  • 2

C

107,230 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator