Can some help me to fix this code, Please (#1 challage)

0 Minh Quan Vu · November 23, 2014
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

main ()
{

    char password[20];
    int check = 0;
    int upper = 0, digit = 0, dollar = 0;

    printf("Enter your password to check it is strong or not\n");
    scanf(" %s", &password);

    for(check = 0 ; check != '\0' ; check++){
        if( isalpha(password[check]) ){
            if( isupper(password[check]) ){
            upper++;
            }
        }

        if( isdigit(password[check]) ){
            digit++;
            }

        if( password[check] == '$' ){
            dollar++;
            }
    }

    if(upper >= 1){
        printf("Capital test:\tPassed\n");
    }else{
        printf("Capital test:\tFailed\n");
    }

    if(digit >= 1){
        printf("Digit test:\tPassed\n");
    }else{
        printf("Digit test:\tFailed\n");
    }

    if(dollar >= 1){
        printf("Dollar test:\tPassed");
    }else{
        printf("Dollar test:\tFailed");
    }

    if (upper >= 1 && digit >= 1 && dollar >= 1){
        printf("\nYour password is strong.\n");
    }else{
        printf("\nYour passowrd is weak.\n");
    }


   return 0;
}


I dont know why even the input has digit, capital letters and dollar sign. The program still say it is weak.

Post a Reply

Replies

Oldest  Newest  Rating
0 Franz Schmidt · November 23, 2014
Your problem is this line:
for(check = 0 ; check != '\0' ; check++){



I understand what you wanted to do:
for(check = 0; password[check] != '\0'; check++){

this would work fine BUT



there is a better way:
for (check = 0; check < strlen(password); check++){

strlen() is a function which counts the chars in password till it reaches '\0' and so you can say how long your pw is.
0 Minh Quan Vu · November 23, 2014
Thank you very much. Can you explain for me why we have to use 
for(check = 0; password[check] != '\0'; check++){


instead of this
for(check = 0 ; check != '\0' ; check++){


Thanks
0 Franz Schmidt · November 23, 2014
Okay this is very easy:
I hope you know that the password variable is a string aka an array.

An array simply said is made of combined variables.
And to check only ONE element/variable of this array you use the index (thats the thing between the "[" and "]")

And '\0' alway symblozies the end of the string. (I'm not sure I think its called null termination character)

The second parameter of the forloop is the abort condition. And if you take the array in there and make the index variable with the counter variable (in this case the check - variable) you can do the loop until the sign in the array is '\0'

And what you did was: you checkt if the check variable (which has the datatype int) isn't equal to '\0' which is equal to 0.
But you check is equal to 0 so the loop won't run a single time.
  • 1

C

107,346 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator