Challenge # 1

+3 Red Man · March 2, 2015
Hello, I'm new to programming.  Bucky specified in his challenge that the password program must check for an upper case letter, a lower case letter, a digit, and the '$' punctuation.

This is my program for Challenge # 1 with comments: Pastebin link here - http://pastebin.com/A88dmHHX


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
/*Password Verification Program tests for 8 to 10 characters, and at least
 *1 upper case letter, 1 lower case letter, 1 digit, 1 '$' punctuation*/

int main()
{
    char userName[50];
    char firstPassword[100];
    char password[100];
    int a = 0;
    int upper = 0, lower = 0, digit = 0, dollarSign = 0;

    printf("Please Create A Username Without Spaces.\n\nNext Create A Password"
           "Between 8 & 10 Characters In Length.\nThe Password Must Include "
           "At Least One Upper Case Letter,\nOne Lower Case Letter, One Digit,"
           "and the '$' Punctuation.\n\n\n");

    printf("Enter Username:    ");
    scanf(" %s", userName);

//Prompts for firstPassword & password then compares strings until they match
    do {
        printf("\nEnter Password:    ");
        scanf(" %s", firstPassword);
        printf("Confirm Password:  ");
        scanf(" %s", password);

        if(strcmp(firstPassword, password) != 0)
            printf("\nPasswords Do Not Match, Try Again.\n\n");
    }while(strcmp(firstPassword, password) != 0);

//Tests for Upper/Lower/Digit/'$' and adds 1 to respective int variables
    while(a <= 10){
    if(isupper(password[a])) {
        upper = 1;
    }
    if(islower(password[a])) {
        lower = 1;
    }
    if(isdigit(password[a])) {
        digit = 1;
    }
    if((password[a] == '$')) {
        dollarSign = 1;
    }
    a++;
    }

/*Test password length 8 to 10 characters
 *Nested If-else tests for password requirements*/
    if (strlen(password) >= 8 && strlen(password) <= 10){
        if(upper && lower && digit && dollarSign){
            printf("\nCongratulations %s Your Password Is Set.\n", userName);
        }
    else{
        printf("\nTry Again, Your Password Needs At Least 1 Upper/Lower/Digit/'$':\n");
    }
    }
//Test password length - too long/short
    if(strlen(password) >= 11){
        printf("\nPassword Is Too Long. Maximum 10 Characters In Length. Try Again.\n\n");
    }
    if(strlen(password) <= 7){
        printf("\nPassword Is Too Short. Minimum 8 Characters In Length. Try Again.\n\n");
    }

return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Jovana Malesevic · March 29, 2015
this is how i did it
i didnt go that much into a details as u did with an username etc
0 Jovana Malesevic · March 29, 2015
/images/forum/upload/2015-03-29/c89fb84d118d5b1b40375471ac2735ca.png
0 viane kyabega · March 6, 2015
Thanks alot(both of you), i understand now what he was doing.
0 Red Man · March 4, 2015
Thank you Mathias,

I'll definitely remember your tip and I'm using it in challenge #2 to improve legibility.  This forum is great for learning, thanks again.



Viane Kyabega ,

int a = 0...   I set the variable a to zero and used it in password(a) to move through the char array password[100].  Functions like, isupper, only test one character at a time so I used a while loop and the increment operator a++.  The while loops 10 times - imagine the a is first 0 then with each a++, it becomes 1,2,3,4,5, and so on, until a = 10.


Arjun Patel,

Thanks for commenting.  Yes, I set password[a] == '$' to test if the password contained a dollar sign.  I first tried with the function, ispunct(), but that made for more code and more problems.
0 Dol Lod · March 3, 2015
Vlane, here is the explanation. Look at the pastebin code again, the loop you are talking about has been modified. 

Think of this as password verification. He is basically making a password consisting of up to 10 ascii characters. He is ensuring that there is at least one uppercase letter, one lowercase letter, one digit and one symbol. At least that's my interpretation of what he is doing. I don't understand the password=='$' but I think he 's trying to check if there is a dollar sign in the password. Essentially he gave criteria to his password and is strictly enforcing those criteria by checking the inputted string as a password. 
0 viane kyabega · March 3, 2015
hey,i am new to programming. down here are some few codes that i did not understand from REDMAN'S source codes


int i = 0;                (i did not understand why you had this variable)...????
    int upper = 0, lower = 0, digit = 0, dollarSign = 0;       (why did you put a zero in the upper and lower since they are letters and not numbers...??,)


while(i <= 10){                                        
    if(isupper(password))
    {   upper = 1;
  }  
if(islower(password)) { 
      lower = 1;   

  if(isdigit(password)) { 
      digit = 1;  
}  
if((password == '$')) {
      dollarSign = 1;
    } 
  i++;  
}

i dont understand the loop. explanation pleaseeeee......!!!!!
0 Mathias Frits Rørvik · March 2, 2015
Hi, your code looks great!

but this line can be changed
 if(upper == 1 && lower == 1 && digit == 1 && dollarSign == 1)

to this
 if(upper && lower && digit && dollarSign)

In C, all other values than 0 is treated as true. It is nice to have in the back of your head, because when you are '&&' together expression they become very large and harder to read.

Good job!
  • 1

C

107,060 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator