0

This code glitches on two things that I cannot diagnose, but is isolated to the if statement: if (strlen(strA) > 8). Suppose I enter in the string "1234567*aA", the program tells me the password is too long, so then I enter in the string "12345", which then triggers the else if statement: else if (strlen(strA) < 9). But everything in the while loop while (j<=9) gets triggered (the program tells me there is a number, a special character, a lowercase letter, and an uppercase letter) and the do/while loop finishes, and the program prompts me to confirm the password. That's wrong. It should prompt me to enter in the password again.

The second issue I notice is if I enter in a string "1111111111111111111", which is obviously too long, the program says the password is too long, but the entire do/while loop terminates and it asks me to confirm my passcode. It should ask me to enter a password again.

If I take out the if statements: if (strlen(strA) > 8) and else if (strlen(strA) < 9), and just run the while loop: while (j<=9), the program works fine, as long as I don't enter in too many characters in the string.

Can anyone diagnose the problem?

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

int main(void) {
    char strA[10];
    char strB[10];
    char strC[] = {'1','2','3','4','5','6','7','8','9','0'};
    char strD[] = {'!','@','#','$','%','^','&','*','(',')'};

    char strE[] = {'a','b','c','d','e','f','g','h','i','j','k',
                   'l','m','n','o','p','q','r','s','t','u','v',
                   'w','x','y','z'};

    char strF[] = {'A','B','C','D','E','F','G','H','I','J',
                   'K','L','M','N','O','P','Q','R','S',
                   'T','U','V','W','X','Y','Z'};
    int i, j, k;
    do {
        k = 0;
        j = 0;

        printf("Please enter your password: ");
        scanf("%s", &strA);
        printf("%s\n", strA);

       if(strlen(strA) > 8) {
           printf("That password is too long\n");
       }
       else if(strlen(strA) < 9) {
           while (j <= 9) {
               for(i = 0; i <= 9; i++) {
                   if(strA[j] == strC[i]) {
                       printf("there is a number in this string.\n");
                       k++;
                       j = 0;
                       while (j <= 9) {
                           for(i = 0; i <= 9; i++) {
                               if(strA[j] == strD[i]) {
                                   printf("there is a character in this string.\n");
                                   k++;
                                   j = 0;
                                   while(j <= 9) {
                                       for(i = 0; i <= 25; i++) {
                                           if(strA[j] == strE[i]) {
                                               printf("there is a lowercase letter in this string.\n");
                                               k++;
                                               j = 0;
                                               while(j <= 9) {
                                                   for(i=0;i<=25;i++) {
                                                       if(strA[j] == strF[i]) {
                                                           printf("there is an uppercase letter in this string.\n");
                                                           k++;
                                                       }
                                                   }
                                                   j++;
                                              }
                                          }
                                      }
                                      j++;
                                  }
                              }
                          }
                          j++;
                      }
                  }
              }
              j++;
          }
          if(k < 4) {
              printf("Your password must contain at least one uppercase letter, one lowercase letter, a number, and a special character.\n");
          }
      }
    } while(k < 4);

    printf("Please confirm your password: ");
    scanf("%s",&strB);

    while(strcmp(strA, strB) != 0) {
        printf("%s\n",strB);
        printf("Your passwords do not match.\nPlease confirm your password: ");
        scanf("%s",&strB);
    }

    putchar('\n');
    printf("%s\n", strA);
    printf("%s\n", strB);

    return 0;
}
6
  • 10
    Holy mother of nesting! o.O Commented Mar 19, 2014 at 19:37
  • 1
    Code looks cryptographic by itself :D Commented Mar 19, 2014 at 19:43
  • 1
    It just has its own system ... ;-) @Yelinna Commented Mar 19, 2014 at 19:44
  • If password lenght is too big, why doesn't break the while loop? It will save one "else" Commented Mar 19, 2014 at 19:46
  • 1
    hahaha. I am a total noob so there is no convention here ;) Commented Mar 19, 2014 at 19:50

2 Answers 2

2

I think following line is creating the problem:

char strA[10];

char strB[10];

Initialize with default values

memset(&strA,'\0', sizeof(strA));
memset(&strB,'\0', sizeof(strB));
Sign up to request clarification or add additional context in comments.

13 Comments

You dropped this: ).
Or just do: char strA[10] = ""; However +1
This worked for the first problem! However, if I put way too many characters in the first string, it still skips out of the do/while loop.
@user3399201: Just exactly tell scanf() what (not) to do: scanf("%9s",&strA); So it will read a maximum of 9 characters, and there will not overflow, and there will not invoke undefined behaviour.
@alk and @user3399201 - the scanf argument should be strA, not &strA. It is the address of the first character that is needed, not the address of the entire array. (On most systems it will appear to work anyway to do &strA, but the correct thing to do is strA, or equivalently, &strA[0]
|
2

Look at ASCII standards : there is a faster way to test if a char c is a digit (c>47 && c<58)! http://en.wikipedia.org/wiki/ASCII

More : look at ctype.h as stated here Determine if char is a num or letter

http://www.cplusplus.com/reference/locale/isalpha/

isalpha(c) : true if letter

isdigit(c) : true if digit

``isupper(c)` : true if upper case

islower(c) : true if lower case

Bye,

Francis

3 Comments

nice, I'll look into it.
These macros work even on a non-ASCII system (i.e. you can use them regardless of what ASCII standards say)
@MattMcNabb: I learned something ! Thanks ! The documentation cplusplus.com/reference/cctype/isalnum states : "In C++, a locale-specific template version of this function (isalnum) exists in header <locale>". But it seems also true in C locale.h: cplusplus.com/reference/clocale states "In <cctype> (<ctype.h>), all functions except isdigit and isxdigit are affected by the extended character set selected."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.