Code:
# include <stdio.h>
#include <stdlib.h>
# define MAX_LIMIT 42
char VALID_CHARS[6] = {'!', '@', '#', '$', '&', '*'};
char VOWELS[15] = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U', '0', '1', '3', '!', '@'};
char CONSECUTIVES_ALLOWED[6] = {'o', 'O', '3', 'e', 'E', '0'};
int check_rule_1 (char *password); //Checks rule 1
int valid_chars (char c); //Check for valid characters
int check_rule_2 (char *password); // Checks rule 2
int check_rule_3 (char *password); //Checks rule 3
int consecutives_allowed (char c); //Checks allowed consecutive characters
int check_rule_4 (char *password); //Checks rule 4
int check_vowel (char c); //Checks for vowels
int check_rule_5 (char *password); //Checks rule 5
int check_valid_char (char c); //Check for valid characters
int check_rule_6 (char *password); //Check rule 6
int file_end(char *x);
int one_rule_violated (int *ruleViolated, int x);
int main (){
FILE *in, *out;
char password[MAX_LIMIT + 1]; // +1 because of null character at end
int rules_check[6], i, rules_violated = 0, x = 1;
in = fopen("passwords.dat", "r");
out = fopen("passwords.out", "w");
if (in == NULL || out == NULL) //Check to see whether files can be openned or not
{
printf("Error opening files!\n");
fclose(in);
fclose(out);
return -1;
}
while (1)
{
for(i = 0; i<6;i++)
rules_check[i]=0;
fscanf(in,"%s", password);
if(file_end(password)) break;
if(check_rule_1(password))
rules_check[0] = 1;
else
rules_violated++;
if(check_rule_2(password))
rules_check[1] = 1;
else
rules_violated++;
if(check_rule_3(password))
rules_check[2] = 1;
else
rules_violated++;
if(check_rule_4(password))
rules_check[3] = 1;
else
rules_violated++;
if(check_rule_5(password))
rules_check[4] = 1;
else
rules_violated++;
if(check_rule_6(password))
rules_check[5] = 1;
else
rules_violated++;
if(rules_violated == 0 )
fprintf(out,"%s is acceptable.\n", password);
else if(rules_violated == 1)
fprintf(out,"%s is not acceptable. Rule %d violated.\n",password,one_rule_violated(rules_check, 6));
else {
fprintf(out,"%s is not acceptable. Rules ", password);
x = 1;
for(i = 0;i<6;i++)
if(rules_check[i] == 0){
fprintf(out, x ? "%d": ",%d", i+1);
x = 0;
}
fprintf(out, " violated. \n");
}
rules_violated=0;
}
return 0;
}
int check_rule_1(char *passWord){
int length = 0;
for(;passWord[length]!='\0';length++);
if(length>=5)
return 1; //If password is ok return 1 or else return 0
else
return 0;
}
int valid_chars (char c){
int i;
for (i = 0; i < 6; i++)
{
if(c == VALID_CHARS[i])
return 1;
else
return 0;
}
}
int check_rule_2 (char *password){
int group[4], i;
group[0]=0; // small case letters
group[1]=0; // upper case letters
group[2]=0; // numbers
group[3]=0; // special characters
for(i=0;password[i]!='\0';i++){
if(password[i]>='A' && password[i]<='Z')
group[0] = 1;
else if(password[i]>='a' && password[i]<='z')
group[1] = 1;
else if(password[i]>='0' && password[i]<='9')
group[2] = 1;
else if(valid_chars(password[i]))
group[3] =1;
}
if(group[0]+group[1]+group[2]+group[3] >= 3) //At least 3 groups must be fulfilled
return 1;
else
return 0;
}
int check_rule_3 (char *password){
int i;
for(i=0;password[i]!='\0';i++){
if(password[i]>='A' && password[i]<='Z');
else if(password[i]>='a' && password[i]<='z');
else if(password[i]>='0' && password[i]<='9');
else if(valid_chars(password[i]));
else return 0;
}
return 1;
}
int consecutives_allowed (char c){
int i;
for (i = 0; i < 6; i++)
{
if (c == CONSECUTIVES_ALLOWED[i])
return 1;
else
return 0;
}
}
int check_rule_4 (char *password){
int i;
for(i=1;password[i]!='\0';i++){
if(password[i-1] == password[i]){
if(consecutives_allowed(password[i]) == 0)
return 0;
}
}
return 1;
}
int check_vowel (char c){
int i;
for (i = 0; i < 15; i++)
{
if (c == VOWELS[i])
return 1;
else
return 0;
}
}
int check_rule_5 (char *password){
int i, no_Of_Vowels=0;
for(i=0;password[i]!='\0';i++)
if(check_vowel(password[i]))
no_Of_Vowels++;
if(no_Of_Vowels>=2)
return 1;
else
return 0;
}
int check_valid_char (char c){
if(check_vowel(c) == 0){
if(c>='A' && c<='Z') return 1;
else if(c>='a' && c<='z') return 1;
else if(c>='0' && c<='9') return 1;
else if(valid_chars(c)) return 1;
else return 0;
}
return 0;
}
int check_rule_6 (char *password){
int i, consecutiveVowels = 0, consecutiveNonVowels = 0;
for(i=0;password[i]!='\0';i++){
if(check_vowel(password[i])){
consecutiveVowels++;
consecutiveNonVowels = 0;
}
else if(check_valid_char(password[i])){
consecutiveNonVowels++;
consecutiveVowels = 0;
}
if(consecutiveVowels == 3 || consecutiveNonVowels == 3)
return 0;
}
return 1;
}
int file_end (char *x){
int length;
for(length=0;x[length]!='\0';length++);
if(length!=3) return 0;
if(x[0]=='E' && x[1]=='N' && x[2]=='D')
return 1;
else
return 0;
}
int one_rule_violated (int *ruleViolated, int x){
int i;
for(i=0;i<x;i++)
if(ruleViolated[i]==0)
return i+1;
}
Input file-