
Patrick B. answered 01/26/20
Math and computer tutor/teacher
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#define MAX_NAME_LENGTH (20)
#define PW_INVALID_SIZE (1)
#define PW_MISSING_UPPERCASE_LETTER (2)
#define PW_MISSING_LOWERCASE_LETTER (4)
#define PW_MISSING_DIGIT (8)
#define PW_MISSING_PUNCTUATION (16)
#define PUNCTUATION_CHARS ("?,.;!:")
/* prompts for a string value: returns the # of bytes and the data Buffer */
int PromptForInput ( char * promptMsg, char * responseBuff, char * titleStr )
{
printf("*******************************************\n");
if (titleStr!=NULL)
{
printf( "%s \n",titleStr);
printf("*******************************************\n");
}
if (promptMsg!=NULL)
{
printf(promptMsg);
}
printf(" :> ");
scanf("%s",responseBuff);
return(strlen(responseBuff));
}
int Names( char * firstName, char * lastName, char * middleName, char * nameBuff)
{
PromptForInput("Please input the first name",firstName,"FIRST NAME");
PromptForInput("Please input the last name",lastName,"LAST NAME");
PromptForInput("Please input the middle name",middleName,"MIDDLE NAME");
sprintf(nameBuff,"%s , %s %s",lastName,firstName,middleName);
printf(" The name is >%s< \n",nameBuff);
return( strlen(nameBuff));
}
int Password( char * pwBuff)
{
PromptForInput("Please input the password",pwBuff,"PASSWORD");
int iReturn = 0;
int uCaseFlag = 0;
int lCaseFlag = 0;
int digitFlag=0;
int punctFlag=0;
int iLoop;
char ch;
int pwLength = strlen(pwBuff);
for (iLoop=0; iLoop<pwLength; iLoop++)
{
ch = pwBuff[iLoop];
if ((ch>=65) && (ch<=90)) { uCaseFlag=1; }
if ((ch>=97) && (ch<=122)) { lCaseFlag=1; }
if ((ch>=48) && (ch<=57)) { digitFlag=1; }
punctFlag = (strchr(PUNCTUATION_CHARS,ch)!=NULL) ? 1 : 0;
}
if ((pwLength <6) || (pwLength>14))
{
iReturn -= PW_INVALID_SIZE;
}
if (uCaseFlag==0) { iReturn -= PW_MISSING_UPPERCASE_LETTER; }
if (lCaseFlag==0) { iReturn -= PW_MISSING_LOWERCASE_LETTER; }
if (digitFlag==0) { iReturn -= PW_MISSING_DIGIT; }
if (punctFlag==0) { iReturn -= PW_MISSING_PUNCTUATION; }
return(iReturn);
} /* Password */
int main()
{
char firstName[MAX_NAME_LENGTH+1];
char lastName[MAX_NAME_LENGTH+1];
char middleName[MAX_NAME_LENGTH+1];
char nameBuff[3*(MAX_NAME_LENGTH+1)];
char pwBuff[255];
Names(firstName,lastName,middleName,nameBuff);
printf(" First name >%s< \n",firstName);
printf(" Last name >%s< \n",lastName);
printf(" Middle name >%s< \n",middleName);
printf(" Name is >%s< \n",nameBuff);
int iReturn = Password(pwBuff);
printf(" password is >%s< \n",pwBuff);
if (iReturn==0)
{
printf(" Password is CORRECT !!! \n");
}
else
{
int X = abs(iReturn);
printf("The Password is Incorrect \n");
if (X / PW_MISSING_PUNCTUATION)
{
printf(" Password must contain one of the following punctuation symbols: %s \n",PUNCTUATION_CHARS);
X %= PW_MISSING_PUNCTUATION;
}
if (X / PW_MISSING_DIGIT)
{
printf(" Password must contain at least one digit \n");
X %= PW_MISSING_DIGIT;
}
if (X / PW_MISSING_LOWERCASE_LETTER)
{
printf(" Password must contain at least one lowercase letter \n");
X %= PW_MISSING_LOWERCASE_LETTER;
}
if (X / PW_MISSING_UPPERCASE_LETTER)
{
printf(" Password must contain at least one uppercase letter \n");
X%= PW_MISSING_UPPERCASE_LETTER;
}
if (X/PW_INVALID_SIZE)
{
printf(" Password must be between 6 and 14 characters \n");
}
} /* password validation */
printf("Press a key...");
while (!getch()) { }
return 0;
}