-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword-validation.cpp
More file actions
131 lines (105 loc) · 3.26 KB
/
password-validation.cpp
File metadata and controls
131 lines (105 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// This program tests a password for the American Equities
// web page to see if the format is correct
// Sheldon Vaughn
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
//function prototypes
bool testPassWord(char[]);
int countLetters(char*);
int countDigits(char*);
int main()
{
char passWord[20];
cout << "Enter a password consisting of exactly 5 "
<< "letters and 3 digits:" << endl;
cin.getline(passWord,20);
if (testPassWord(passWord))
cout << "Please wait - your password is being verified" << endl;
else
{
cout << "Invalid password. Please enter a password "
<< "with exactly 5 letters and 3 digits" << endl;
cout << "For example, my37RuN9 is valid" << endl;
}
int numLetters = countLetters(passWord);
int numDigits = countDigits(passWord);
cout << "There are " << numLetters <<" letters in the password" << endl;
cout << "There are " << numDigits <<" digits in the password" << endl;
bool valid = testPassWord(passWord);
if (valid != 0)
{
cout << "Password Accepted" << endl;
cout << "Your password is now: " << passWord << endl;
}
else
{
cout << passWord << " is an invalid password" << endl;
}
return 0;
}
//**************************************************************
// testPassWord
//
// task: determines if the word contained in the
// character array passed to it, contains
// exactly 5 letters and 3 digits.
// data in: a word contained in a character array
// data returned: true if the word contains 5 letters & 3
// digits, false otherwise
//
//**************************************************************
bool testPassWord(char custPass[])
{
int numLetters, numDigits, length;
length = strlen(custPass);
numLetters = countLetters(custPass);
numDigits = countDigits(custPass);
if (numLetters == 5 && numDigits == 3 && length == 8 )
return true;
else
return false;
}
// the next 2 functions are from Sample Program 10.5
//**************************************************************
// countLetters
//
// task: counts the number of letters (both
// capital and lower case in the string
// data in: a string
// data returned: the number of letters in the string
//
//**************************************************************
int countLetters(char *strPtr)
{
int occurs = 0;
while(*strPtr != '\0')
{
if (isalpha(*strPtr))
occurs++;
strPtr++;
}
return occurs;
}
//**************************************************************
// countDigits
//
// task: counts the number of digitts in the string
// data in: a string
// data returned: the number of digits in the string
//
//**************************************************************
int countDigits(char *strPtr) // this function counts the
// number of digits
{
int occurs = 0;
while(*strPtr != '\0')
{
if (isdigit(*strPtr)) // isdigit determines if
// the character is a digit
occurs++;
strPtr++;
}
return occurs;
}