Skip to content

Instantly share code, notes, and snippets.

@Sebbyastian
Created December 15, 2014 21:58
Show Gist options
  • Save Sebbyastian/42b17424d5fbb41ec43d to your computer and use it in GitHub Desktop.
Save Sebbyastian/42b17424d5fbb41ec43d to your computer and use it in GitHub Desktop.
BR password challenge (using scanf)
int main(void) {
int lower_length = 0,
upper_length = 0,
digit_length = 0,
length = 0;
puts("Please enter a password and I'll judge its security...");
# define LOWER_FMT "%*[qwertyuiopasdfghjklzxcvbnm]%n"
# define UPPER_FMT "%*[QWERTYUIOPASDFGHJKLZXCVBNM]%n"
# define DIGIT_FMT "%*[1234567890]%n"
scanf(LOWER_FMT, &lower_length);
scanf(UPPER_FMT, &upper_length);
scanf(DIGIT_FMT, &digit_length);
if (scanf(LOWER_FMT, &length) == 0) { lower_length += length; length = 0; }
if (scanf(UPPER_FMT, &length) == 0) { upper_length += length; length = 0; }
if (scanf(DIGIT_FMT, &length) == 0) { digit_length += length; length = 0; }
if (scanf("%*s%n", &length) != 0) { length = 0; }
length += lower_length + upper_length + digit_length;
# define AT_LEAST "Your password must contain at least "
puts(lower_length == 0 ? AT_LEAST "one lower alpha character"
: upper_length == 0 ? AT_LEAST "one upper alpha character"
: digit_length == 0 ? AT_LEAST "one decimal digit character"
: length < 8 ? AT_LEAST "eight characters"
: "Your password seems secure to me!");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment