I have this code, which reads in a string password from the user and checks if it has a dollar sign $
, a number, and an upper case letter. If it has all three, it prints:
Your password is correct
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXSIZE 100
void password_read(char password[]);
int password_check(char password[]);
int
main(int argc, char *argv[]){
char password[MAXSIZE];
int sum;
password_read(password);
sum = password_check(password);
if (sum) {
printf("Your password is correct\n");
} else {
printf("Your password sucks\n");
}
return 0;
}
void
password_read(char password[]) {
printf("Enter password:\n");
scanf("%s", password);
}
int
password_check(char password[]) {
int upper = 0;
int digit = 0;
int dollar = 0;
int i;
for (i = 0; i < strlen(password); i++) {
if (isupper(password[i])) {
upper = 1;
}
else if (isdigit(password[i])) {
digit = 1;
}
else if (password[i] == '$') {
dollar = 1;
}
}
return (upper && digit && dollar);
}
I'm pretty shabby with C and I was just wondering if there was a way to reduce part of the code. I also just want to check if the pointer/array handling is correct, since I've only just started using it properly.