password testing program (newbie needs help)

0 Raj Dev · March 12, 2015
I'm on tutorial 33. and the challenge was to create a program to test passwords with uppercase+lowercase+digit and '$' sign.
:( My own program just doesn't turn out right. So I found this program below in the youtube comment section. can you please explain it?  




int main()
{
    char password[14];

    printf("Enter your password (Max 15 characters, include $ sign, one uppercase letter and a number) : \n\n");
    scanf(" %s", password);

    int a, b, c;
    a = b = c = 0;

    int counter;

    for(counter=0;counter<=14;counter++){
        if(password[counter]=='$'){
            a++;
        }
        if( isupper(password[counter])){
            b++;
        }
        if( isdigit(password[counter])){
            c++;
        }
    }

    if((a>0) &&(b>0) &&(c>0)){
        printf("\nYour password is good to go!\n");
    }else{
        printf("\nYour password is missing either a $ sign, one uppercase number and/or a number.\n");
    }

    return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Dol Lod · March 12, 2015
To begin with that code is flawed. 

Here is the corrected code.


int main()
{
char password[16];
int a,b,c,counter;
do
{
for(counter=0;counter<16;counter++)
{
password[counter]='\0';
}
printf("Enter your password (Max 15 characters, include $ sign, one uppercase letter and a number) : \n\n");
   scanf(" %s", password);
a=0;
b=0;
c=0;
for(counter=0;counter<=14;counter++)
{
if(password[counter]=='$')
a++;
if( isupper(password[counter]))
b++;
if( isdigit(password[counter]))
c++;
}
if((a>0) &&(b>0) &&(c>0))
{
printf("\nYour password is good to go!\n");
break;
}
else
printf("\nYour password is missing either a $ sign, one uppercase number and/or a number.\n");
}while(1);
return 0;
}

Anyway, the first part initializes a character array to store the password in.  Then I reset all characters in the array to the null byte to make sure I only get the password that was entered. The password is then entered through scanf  Next, this loop continues to check each character in the password to see if it has a $, uppercase letter, and a number by using a,b,c as flags. Then if the password is valid, the program exits after printing out the password is good to go. Otherwise, a warning is given saying that the password was invalid in some way. 
0 Raj Dev · March 13, 2015
I'm a bit confused on '\0' part...      password[counter]='\0';
0 Raj Dev · March 13, 2015
Thank you... I was looking for a book :)
0 Dol Lod · March 13, 2015
\0 is the null byte. In c-strings or character arrays, the null byte is typically used to mark off the end of an identifier. So I'm basically ensuring that no additional elements are printed if you want to print out the password at a later time. 
  • 1

C

106,929 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator