That's my code (CHALLENGE #1) .. need assist

0 Hazem Mekawy · September 25, 2015
whenever i enter 1 symbol .. it appears as 2 symbols ! 


int main()
    int i=0;
    char YourPassword[30];
    int uppercase=0;
    int lowercase=0;
    int symbols=0;
    int digits=0;

    printf("Enter Your password here..\n");



        }else if(isdigit(YourPassword)){


    if(uppercase!=0 && lowercase!=0 && digits!=0 && symbols!=0 && i!=0){
        printf("Your password is good\n");

        printf("Your password ain't good\n");
    printf("Your password was(%s):and it was %d uppercases,%d lowercases,%d digits,%d symbols \n",YourPassword,uppercase,lowercase,digits,symbols);
return 0 ;

Post a Reply


Oldest  Newest  Rating
+1 Bram Dekker · September 25, 2015
According to  the strln outputs the amount of characters for example: "hi" would be two. You are counting from 0, that would mean that you will always do one extra loop (0, 1, 2). Perhaps strln - 1 helps?
+1 Stefan Tuny · September 25, 2015
also keep attention when reading strings with scanf(). It does not check for length which means you can get error when entered password is too long. better use fgets() function for that.

also as Toph mentioned, checking for uppercase / lowercase you have to reference every letter. You will also have to start your loop at 0 and end at strlen(string) - 1

for(i = 0; i < strlen(YourPassword) - 1; i++) {
   if(isalpha( YourPassword  )) {...}
0 c student · September 26, 2015
though most of you have solutions that "fix" the problem none of you have properly identified the cause of the bug.  let's say you have an input: "F00b@r".  in the array, it would look something like this:
[F][0][0][b][@][r][\0]    character elements in the array
[0][1][2][3][4][5][6 ] array indices

the length of this string is 6.  when you use your loop here
for(i = 0; i < = strlen(YourPassword); i++)

at some point, your condition will be:
i == strlen(YourPassword);    // i == 6

from above,

YourPassword[6] == '\0';    // what is this? a character? a digit?
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator