- As a first step, fix the indentation.
- As a second step, eliminate superfluous blocks, and use a block if a loop / if / else contains anything longer.
- Next, look at your comments. They just restate the code badly, and are thus worse than useless. Comments should be used to explain why something is done, or to express pre- and post-conditions as well as return-values, if it cannot be done acceptably in code.
- Don't abbreviate names more than neccessary to make them useable. Descriptive names are essential to making code self-explaining.
- Add a space around every binary operator, and after a comma.
- Avoid over-long lines. Horizontal scrolling, especially combined with vertical scrolling, kills readability.
- Extract the evaluation of the password into its own function for reusability.
- Test every function which can fail for failure.
- Don't read an unknown amount of data into a finite buffer. Buffer-overflows are bad.
- Don't pass a pointer to a pointer to
char
where a pointer tochar
is expected. - Be aware that passing anything but the value of an
unsigned char
orEOF
to the character-classification-functionsisdigit
andisupper
is undefined behavior. Cast tounsigned char
. - Make sure to end your output with a newline, or the next prompt in the terminal will be in a weird position.
- You don't use anything from
<conio.h>
so don't include it. - As an aside, don't you also want a lower-case letter?
Applying all that yields:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
static int validPassword(const char* password) {
int hasUpper = 0, hasDigit = 0, hasDollar = 0 /*, hasLower = 0*/;
const unsigned char* p = (const unsigned char*)password;
while(*p) {
if(isupper(*p))
hasUpper = 1;
/*else if(islower(*p))
hasLower = 1;*/
else if(isdigit(*p))
hasDigit = 1;
else if(*p == '$')
hasDollar = 1;
}
return hasUpper && hasDigit && hasDollar /* && hasLower*/;
}
int main() {
char password[32];
puts("Please enter a password having an upper case letter, a number"
" and also a $ sign: ");
if(scanf("%31s", password) != 1) {
puts("Could not get the password.");
return EXIT_FAILURE;
}
if(strlen(password) > 30) {
puts("Password is too long.");
return EXIT_FAILURE;
}
if(!validPassword(password)) {
puts("Password must contain an upper case letter, a number and a $ sign.");
return EXIT_FAILURE;
}
puts("Nice Password!");
return 0;
}