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
0 Tobi Okubanjo · July 11, 2015
Thanks a lot. I see now why it's not working. You were a great help. I really appreciate it. 
0 Gitesh Khanna · July 7, 2015
is it actually working? i tried it and it allows me to keep the password even if i miss one condition of them all. 
0 Bernhard Riemann Georg Friedrich · July 7, 2015
thanks
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
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 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 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 peymans johans · July 6, 2015
http://www.gowebsite.ir
0 c student · July 6, 2015
default value for a character?  i'm not sure what you mean
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?
  • 1
  • 2

C

107,225 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator