Challenge 1....Please help unable to find the error

0 Drupad Preenja · May 20, 2015
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>


int main()
{
    char password[20];
    int lower=0,upper=0,digit=0,dollar=0;
    int i;
    printf("Enter your password: \n");
    scanf(" %s", &password);

    int sizeOfPassword = sizeof(password);

    for( i = 0; i < sizeOfPassword ; i++ ){
        if( isalpha (password))
        {
            if( isupper (password)){
                upper++;
            }else if( islower (password)){
                lower++;
            }

        }
        if(isdigit(password)){
            digit++;
        }
        if(password=='$'){
            dollar++;
        }
    }
    if( lower>=1 && digit>=1 && dollar>=1 && upper>=1 )
    {
        printf("your password is according to the guidelines");
    }else{
        printf("your password is incorrect...Please !!see the guidelines");
    }


    return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Superman HC · May 24, 2015
@Drupad: Two mistakes
1. While using if condition each char is to be checked and not the whole password, hence if(isupper(password)) is to be used
2. For the special character problem, the logic is wrong as it checked for the whole password to contain only a single letter as '$'

I am attaching your whole code modified.. you can refer...


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


int main()
{
char password[20];
int lower=0,upper=0,digit=0,dollar=0;
int i;
printf("Enter your password: \n");
scanf(" %s", &password);

int sizeOfPassword = sizeof(password);

for( i = 0; i < sizeOfPassword ; i++ ){
if( isalpha (password))
{
    if( isupper (password)){
    upper++;
    }else{
    lower++;
    }
}
else if(isdigit(password)){
digit++;
}
else{
dollar++;
}
}
if( lower>=1 && digit>=1 && dollar>=1 && upper>=1 )
{
printf("your password is according to the guidelines");
}else{
printf("your password is incorrect...Please !!see the guidelines");
}


return 0;
}
0 Ghufran Khan · May 22, 2015
Ah.. sorry i found my mistakes..
0 Ghufran Khan · May 21, 2015
Hi, I have some bug in Challenge 1, My whole program is compiling and running perfectly except for two things:

1- It prints "perfect" even when there is no small/ large case letter or number along with the message that " your password should has a .... ".

2 - If it is missing $ sign or a number/lower/upper case, the " your password should has..." message is printed twice...


NOTE: My password also checks for lower case.


Here is my code...




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

int main()
{
    //you can use ints and characters interchangeably in most cases
    char pass[10]; // b, 7, $

printf("Enter your password:  ");
scanf("%s", pass);
    int n=0;
    int uper = 0;
    int down = 0;
    int num = 0;
    int dollar = 0 ;


for (n=0; n< strlen(pass); n++)
{



    if( isalpha(pass[n]) ){

        if( isupper(pass[n]) ){


           uper++;


        }else{
            down++;

        }

    }else if( isdigit(pass[n]) ){
               num++;

    }
        else if( pass[n] == '$')
        {

dollar++;


        }

    }




if (uper == 0)

   {

   printf("Your password should has an upper case letter! \n ");

   }
if (down == 0)

   {

   printf("Your password should has a down case letter! \n ");}

if (num == 0)

   {

   printf("Your password should has a number! \n ");}

if (dollar == 0)

   {

   printf("Your password should has a dollar sign \n ");}


if (down == 0)

   {

   printf("Your password should has a down case letter! \n ");}

if (num == 0)

   {

   printf("Your password should has a number! \n ");}

   if (dollar == 0)

   {

   printf("Your password should has a dollar sign \n ");}

   else {
    printf(" Perfect"  );
   }

    return 0;
}
0 Oguz AKKAS · May 20, 2015
One problem:  if( isalpha (password)) do instead if( isalpha (password))
  • 1

C

107,313 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator