c challenge #1

+2 Rona Artuz · January 21, 2016
Hi this is my code for password creation and verification, please help me to create a short and nice clean code for this, by the way this is working , I just want to know if there's another way to make it short.


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

int main()
{

 char password [17];
 int digit=0;
 int upper=0;
 int ascii=0;


 int passSize;

 passSize= strlen(password);

 puts("Please create your password: \n");
 puts("Password should contain alphanumeric characters MAX 8 characters, (numbers, symbols, uppercase and lowercase : \n");
 fgets( password,20,stdin);

if(passSize<=8){


 if(isdigit(password[0])){

   digit=1;
}
else if(isupper(password[0])){
    upper=1;
}

else if(isascii(password[0])){
   ascii=1;
   }
  //*********************************//
 if(isdigit(password[1])){

   digit=1;
}
else if(isupper(password[1])){
    upper=1;
}

else if(isascii(password[1])){
   ascii=1;
   }

//*******************************//
    if(isdigit(password[2])){

   digit=1;
}
else if(isupper(password[2])){
    upper=1;
}

else if(isascii(password[2])){
   ascii=1;
   }

//*******************************//
   if(isdigit(password[3])){

   digit=1;
}
else if(isupper(password[3])){
    upper=1;
}

else if(isascii(password[3])){
   ascii=1;
   }
//*******************************//
   if(isdigit(password[4])){

   digit=1;
}
else if(isupper(password[4])){
    upper=1;
}

else if(isascii(password[4])){
   ascii=1;
   }
//*******************************//
    if(isdigit(password[5])){

   digit=1;
}
else if(isupper(password[5])){
    upper=1;
}

else if(isascii(password[5])){
   ascii=1;
   }

//*******************************//
   if(isdigit(password[6])){

   digit=1;
}
else if(isupper(password[6])){
    upper=1;
}

else if(isascii(password[6])){
   ascii=1;
   }

   //*******************************//
   if(isdigit(password[7])){

   digit=1;
}
else if(isupper(password[7])){
    upper=1;
}

else if(isascii(password[7])){
   ascii=1;
   }

if(digit==1 && upper==1 && ascii==1 ){
                           puts("You're password is good to go! \n");
                                    }


            else{
    puts("You're password doesn't meet the requirements needed, please try again! \n");
                           }

    return 0;
}
}


Post a Reply

Replies

Oldest  Newest  Rating
0 Supun Rangana · February 8, 2016
Why this isnt working...??

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

int main()
{
    char pswrd[9];
    int u=0;
    int d=0;
    int s=0;
    int X=0;

    printf("Enter your password.? (8)");
    scanf(" %s",&pswrd);

    printf("wait\n");

    for(X=0;X=7;X+=1){
        if(isupper(pswrd[X])){
            u++;

        }else if (isdigit(pswrd[X])){
            d++;

        }else if ((pswrd[X]='$')){
            s++;
        }
        X++;

    }




            if ((u>=1)&&(d>=1)&&(s>=1)){
            printf("strong password");
            }else {
            printf("weak password");
}
            return 0;
}
0 Linguist Llama · February 4, 2016
MrHobby Bot, you're making it too hard. Go back and learn about logic. Here's some example code.

#include <stdio.h>
#include <ctype.h>

int main(void){   
    int alpha = 0, digit = 0, punct = 0, c;
    putsf("Enter a password:");

    do {
        c = getchar();
        alpha = alpha || isalpha(c);
        digit = digit || isdigit(c);
        punct = punct || ispunct(c);
    } while (c != EOF && c != ' ');
   

    puts(alpha && digit && punct ? "Password accepted!" : "Password cannot be used!");
}



You're making it too hard. Go back and learn about end user satisfaction. Here's some example code:

#include <stdio.h>
#include <ctype.h>

int main(int argc, char **argv) {
    if (argc <= 1) {
        puts("Usage: ./app <password>");
        return 0;
    }
   
    int alpha = 0, digit = 0, punct = 0;
    for (size_t x = 0; argv[1][x]; x++) {
        unsigned char c = argv[1][x];
        alpha = alpha || isalpha(c);
        digit = digit || isdigit(c);
        punct = punct || ispunct(c);
    }
   
    puts(alpha && digit && punct ? "Password accepted!" : "Password cannot be used!");
}
0 MrHobby Bot · January 25, 2016
@Linguist Llama
Ah, you're right. Fixed.
0 Linguist Llama · January 25, 2016
@MrHobby Bot Your code is faulty.

Suppose you have an array of 1 char named foo, are both foo[0] and foo[1] valid? How can this be, when the array is only one char?

Suppose you have an array of 30 char named bar, are both bar[0] and bar[30] valid?...

Please, if you're going to put yourself in the position of a teacher, make sure you don't make the common mistakes of a newcomer.
0 MrHobby Bot · January 24, 2016
You're making it too hard. Go back and learn about loops. Here's some example code:

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

int main(){
    char userPassword[30];
    printf("Enter a password: ");
    scanf(" %s", userPassword);

    int trueA, trueB, trueC;
    trueA = trueB = trueC = 0;
    int passLoop = 0;
    while (passLoop != 31) {
        if (isdigit(userPassword[passLoop]) == 1) {trueA = 1;}
        if ((isalpha(userPassword[passLoop]) == 1)&&(isupper(userPassword[passLoop]) == 1)){trueB = 1;}
        if (userPassword[passLoop] == '$' ) {trueC = 1;}
        passLoop++;
    }

    if ((trueA == 1)&&(trueB == 1)&&(trueC == 1)) {printf("Password accepted!");}
    else {printf("Password cannot be used!");}

    return 0;
    }
0 Linguist Llama · January 23, 2016
You seem to have missed the point of the exercise, Rona. It's meant to test your understanding of loops and strings.

Aside from misunderstanding those key concepts (which allows you to make mistakes like the one Krooty pointed out), there are some other obvious errors, such as telling fgets to store 20 characters into an array that can only hold 17, and some less obvious errors... I might mention those on a different day.

Since these tutorials aren't working for you, have you considered:

1. Learning a different language. C isn't particularly "newb-friendly"; it's got a concept known as undefined behaviour which allows you to write faulty code which might give you the impression that "this is working" initially, and then start failing to work later. I can recommend Python or Javascript, as these don't have this concept.

2. Learning from a book that's been tested by time and proven to successfully teach people year after year for decades, such as K&R second edition... This book isn't for people who are new to the world of programming, and I don't think you'd make it through it at the moment.
0 Laura Lee · January 21, 2016
You're checking the length of the string before you retrieve it from the user.
Also you have no idea what value an address may contain when you declare it.
  • 1

C

107,324 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator