Help in Challenge One?

0 Jack Mead · March 16, 2015
My code seems to look a bit different from everyone elses. If anyone could help out that would be wonderful!

char npw[10];
int a, b;
a=b=0;
    attempts = 0;
do{
        printf("\nCreate a new password.\n");
        printf("> ");
        scanf("%s", npw);

        if(isdigit(npw[0]) || isdigit(npw[1]) || isdigit(npw[2]) || isdigit(npw[3]) || isdigit(npw[4]) || isdigit(npw[5]) || isdigit(npw[6]) || isdigit(npw[7]) || isdigit(npw[8]) || isdigit(npw[9])){
            a=1;
        }
        if(isupper(npw[0]) || isupper(npw[1]) || isupper(npw[2]) || isupper(npw[3]) || isupper(npw[4]) || isupper(npw[5]) || isupper(npw[6]) || isupper(npw[7]) || isupper(npw[8]) || isupper(npw[9])){
            b=1;
        }
        if(a==0 || b==0 || strlen(npw)<10){
            printf("Invalid. Include 10 characters, upper case letters, and numbers");
            attempts++;
            continue;
        }
        if(a==1 && b==1 && strlen(npw)>=10){
            printf("Your new password is '%s'", npw);
            break;
        }

}while(attempts<=1000000000000000);
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Dol Lod · March 17, 2015
Read about how to use for loops. If you're dealing with this many changes. If you are doing it for performance, it is just not worth it. Readability is a plus of using a for loop and the other thing is that you can easily change the size without needing to do a whole bunch of modifications. 

Additionally, codetags work if you only start writing code from when you edit your post.

Next, make your code look neat,putting braces on next line, ...

Now, if there is an obvious problem, at least you won't miss it. 

while(attempts<=1000000000000000); If you want infinity, just say while(1) instead to make it run forever. 
0 Jacky Leung · March 29, 2015
It looks like hard-coding! Use for loop instead of the OR operator (||) .
Moreover, check out the ways of constructing an infinity loop by using while loop!
One more thing, you can break the for loop when the next character is unassigned.

Check out my code:

#include <stdio.h>

int main()
{
    char pw[20];
    int uppercase = 0;
    int number = 0;
    int dollarsign = 0;
    int i;

    do{
        printf("Enter your password: \n");
        scanf(" %s", pw);

        for (i = 0; i < 20; i++){
            if (isupper(pw))
                uppercase = 1;
            if (isdigit(pw))
                number = 1;
            if (pw == '$')
                dollarsign = 1;
            if (pw[i+1] == NULL)
                break;
        }

        if (uppercase == number == dollarsign == 1){
            printf("Strong password: %s", pw);
            break;
        }
        else printf("Weak password! Try again! \n");
    }while (1);

    return 0;
}
  • 1

C

106,970 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator