Can someone help me to fix this code (Challenge #1)

+2 Minh Quan Vu · November 15, 2014
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char password[20];
    int i, upper = 0, digit = 0, dolarsign = 0;

    printf("Enter your password. This program will check it is strong or not \n");
    scanf("%s", password);

    for(i = 0; i <= 20; i++){
        if( isalpha(password) ){
                if( isupper(password) ){
                    upper++;
                }
            }
        if( isdigit(password) ){
            digit++;
        }else{
            dolarsign++;
        }
        if(upper>=1 && digit>=1 &&dolarsign>=1){
            printf("Your password is strong!\n");
        }else{
            printf("Your password is not strong enough!\n");
        }
    }


return 0;
}



Post a Reply

Replies

Oldest  Newest  Rating
-1 Minh Quan Vu · November 16, 2014
It doesn't work. Whatever my password is, it just shows "your password is not strong" for several times
0 Minh Quan Vu · November 17, 2014
I dont know why it shows "your password is not strong enough" for many times, I just need it shows one time. Can you help me to fix this issue ?
0 Minh Quan Vu · November 17, 2014
I looked at your code but as you can see in the output it showed "Your password is strong" for 5 times then after that is "Your password is not strong" 
0 Mathias Frits Rørvik · November 18, 2014
First off, you need to include ctype.h.

isalpha, isdigit, isupper checks ONE character, not an entire array of characters. you need to check every place in the array. Also you are printing your password is strong or your password is not strong enough every time you loop.


Here is one possible solution I wrote.
/* Copyright <Mathias F. Rørvik> */

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

int main(void) {
    char password[20];
    int i;
    int upper = 0, digit = 0, dollar = 0;

    if (fgets(password, sizeof(password), stdin) == NULL) {
exit(EXIT_FAILURE);
    }
    
    for (i = 0; i < strlen(password); i++) {
if (isupper(password)) {
   upper++;
} else if (isdigit(password)) {
   digit++;
} else if (password == '$') {
   dollar++;
}
    }

    if (upper && digit && dollar) {
printf("Your password is strong");
    } else {
printf("Your password is weak");
    }

    return 0;
}
0 Minh Quan Vu · November 18, 2014
Thank you guys. My code is working now :D
  • 1

C

103,491 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator