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
0 alexander starbuck · February 22, 2015
Aleksander, 

first of all you've got a great name! :) 

I tried compiling and running your code in my XCode and these are the warnings I got (and messages from the debugger one it broke down). Since I am a total newbie I can't interpret what my debugger is saying since it's got something to do with the memory and registers and is all in hex... maybe Bucky or some of the other more experienced guys and gals can contribute.

Cheers!
Alex

/images/forum/upload/2015-02-22/93f995f327de3b6ba14d5d0f31c2640b.png
0 c student · January 24, 2015
here is your gets() function:
warning: the `gets' function is dangerous and should not be used.
it is still susceptible to overflowing

also, you attempt to return a value where main has no declared return type.
0 Lazar Bulic · January 24, 2015
Boom my first code here. Here is the improved version of your code :) 

thing i changed: 

I don't like scanf when using strings and characters. I always use getchar() and gets()
Also i changed your variable declaration its more neater now.
I removed the variable that counts the length of string as you have a function strlen()
Also removed libraries that aren't used.
Also i added goto COMEBACK if user passwords don't math, this way he can make more mistakes and correct them. Your code just game an option to make mistake once.


I would remove variable index and do the check with a pointer on string (would declare your strings like pointers char *passowrd[21]) but dnk if your are familiar with pointers so i didn't use that.


#include <stdio.h>
#include <string.h>

main()
{
    char password[21], rpassword[21], decision;
    int index, number=0, big=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: ");
    gets(password);
    printf("\nPlease repeat your password: ");
    gets(rpassword);
    if(strcmp(password, rpassword) != 0){
        printf("Yuor password doesn't match! Try again!\n");
        goto COMEBACK;
    }

    for(index=0; index<=strlen(password); index++){
        if(isupper(password[index]))
            big++;
        else{
            if(isdigit(password[index]))
                number++;
        }
    }

    printf("%d %d %d",big, number, strlen(password));

    if((big > 0) && (number > 0) && (strlen(password) >= 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): " );
        decision = getchar();
        switch(decision){

        case 'y' :
        index=0;
        big=0;
        number=0;
        fflush(stdin);
        goto COMEBACK;

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

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

    }

    getchar();
    return 0;
}
0 Aleksander Olewinski · January 22, 2015
I have just checked my code. It is fine. Oh Gosh I will kill ya :). I tought I made mistake! Thanks again c student!
0 Mathias Frits Rørvik · January 22, 2015
Jeeze, that is retarded. Sorry, my mistake. I was not aware of the bug. 
0 Aleksander Olewinski · January 22, 2015
Opss! Thanks mates.
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.
0 Mathias Frits Rørvik · January 21, 2015
Tell me at which point do you check every position? Your program doesn't even quit, it is stuck in an infinite loop. Have you tried running your own program? You are incrementing the variable i, but you are not checking password at position i

isdigit, isupper expects a character as a parameter. What is the type of password? It is a character array. 
while(password[index] != '\0') {
  if(isupper(password[index)){
      big+=1;
      i++;
  }
  else if(isdigit(password[index])){
      number+=1;
     i++;
  }
  else {
     i++;
  }
}

When you compile the program, don't you get any warnings?
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 Mathias Frits Rørvik · January 21, 2015
This is not working correctly, you are calling isdigit/upper/lower() on password, and not password. By not doing this you are ONLY checking the first character of password. 

Bucky, why do you say that the code is correct, when it clearly it is not? 
  • 1
  • 2

C

106,980 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator