Reputation: 47
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;
}
Upvotes: 0
Views: 215
Reputation: 9817
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
Upvotes: 2
Reputation: 5731
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));
Upvotes: 2