Challenge 1# Completed

+3 Aleksander Olewinski · January 11, 2015
Hello. Here is my first challenge :)


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

int main()
{
    char password[21];
    char rpassword[21];
    char decision;
    int i=0;
    int big=0;
    int number=0;
    int howLong=0;

    printf("Welcome. Before you type your password please read this carefully!\nPassword should have minimum length of 6 and maximum length of 20 characters.\nPassword should include one or more: capital letter and number.");
    COMEBACK:printf("\nPlease enter your password: ");
    scanf(" %s", password);
    printf("\nPlease repeat your password: ");
    scanf(" %s", rpassword);
    if(strcmp(password, rpassword) !=0){
        printf("\nPassword does not match repeated password. Please try again.");
        printf("\nPlease enter your password: ");
        scanf(" %s", password);
        printf("\nPlease repeat your password: ");
        scanf(" %s", rpassword);
    }

    while(password!='\0'){
        if(isupper(password)){
            big+=1;
            howLong+=1;
            i++;
        }
        else if(isdigit(password)){
            number+=1;
            howLong+=1;
            i++;
        }
        else{
            i++;
            howLong+=1;
        }
    }

    printf("%d %d %d",big, number, howLong); //just checking if it is working :)

    if((big>0) && (number>0) && (howLong>=6)){
        printf("\nYour password match the criteria!");
    }
    else{
        printf("\nYour password does not match the criteria! Try again!");
        printf("\nDo you want try enter your password again? (y/n): " );
        scanf(" %c", &decision);
        switch(decision){

        case 'y' : howLong=0;
        int i=0;
        int big=0;
        int number=0;
        goto COMEBACK;

        case 'n' : printf("\nSee you soon");
        break;

        default : printf("\nI do not know what does this mean. Bye!");
        break;}

    }
    
    getchar();
    return 0;
}

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
+2 Bucky Roberts · January 11, 2015
0 Aleksander Olewinski · January 11, 2015
Thanks Bucky :). You are very good teacher!
0 c student · January 12, 2015
http://i.imgur.com/eGbg7eC.png
it doesn't matter if you're telling someone you can only have a max number of input, if it can break, it will break.
0 Aleksander Olewinski · January 13, 2015
Hi c student. Your are right. It is not completely dumbuserproof. I am thinking about solutions for this issue. Maybe you have some ideas?
 
0 c student · January 14, 2015
it's not that hard, find the problem and do something about it, or completely eliminate the problem with a better alternative.
-1 Aleksander Olewinski · January 16, 2015
c student! I will give you challenge. Try to break my program now :).

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

int main()
{
    char password[21];
    char rpassword[21];
    char decision;
    int i=0;
    int big=0;
    int number=0;
    int passwordlen;

    printf("Welcome. Before you type your password please read this carefully!\nPassword should have minimum length of 6 and maximum length of 20 characters.\nPassword should include one or more: capital letter and number.");
    COMEBACK:printf("\nPlease enter your password: ");
    scanf(" %s", password);
    passwordlen = strlen(password);
    while(passwordlen < 6){
        printf("Your password is too short! Try again!");
        printf("\nPlease enter your password: ");
        scanf(" %s", password);
    }
    while(passwordlen >20){
        printf("Your password is too long! Try again!");
        printf("\nPlease enter your password: ");
        scanf(" %s", password);
    }
    printf("\nPlease repeat your password: ");
    scanf(" %s", rpassword);
    if(strcmp(password, rpassword) !=0){
        printf("\nPassword does not match repeated password. Please try again.");
        printf("\nPlease enter your password: ");
        scanf(" %s", password);
        printf("\nPlease repeat your password: ");
        scanf(" %s", rpassword);
    }

    while(password!='\0'){
        if(isupper(password)){
            big+=1;
            i++;
        }
        else if(isdigit(password)){
            number+=1;
            i++;
        }
        else{
            i++;
        }
    }

    printf("%d %d", big, number); //just checking if it is working

    if((big>0) && (number>0)){
        printf("\nYour password match the criteria!");
    }
    else{
        printf("\nYour password does not match the criteria!\n It should have at least one big letter and one digit.\n Try again!");
        printf("\nDo you want try enter your password again? (y/n): " );
        scanf(" %c", &decision);
        switch(decision){

        case 'y' : i=0;
        big=0;
        number=0;
        goto COMEBACK;

        case 'n' : printf("\nSee you soon");
        break;

        default : printf("\nI do not know what does this mean. Bye!");
        break;}

    }

    getchar();
    return 0;
}


Good luck!
+2 c student · January 17, 2015
challenge accepted
messing up your buffers:
http://i.imgur.com/3ymsElt.png

finishing you off with a classic segmentation fault:
http://i.imgur.com/khJdfmR.png

flawless victory... fatality.   8-)
0 Aleksander Olewinski · January 17, 2015
Wow... That is a punch. I am begging for mercy ;). I see there is many things to learn to make the program completely unbreakable. Thank you for your advice about posting my code :D. You win.  
0 Aleksander Olewinski · January 21, 2015
?????????
Mathias, with all respect. In my code 'while' loop is checking every single character until it reach end of string. God help me but I do not understand what is your point. If anything is wrong in your opinion explain it like c student did!
Maybe you just mistook it with put and gets method? Just can You explain it clearly?
0 c student · January 21, 2015
there's a bug where you cant see the array element i because it's read as an italics tag.  look at your own comment.
  • 1
  • 2

C

122,687 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator