forked from ChicoState/UnitTestPractice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPassword.cpp
More file actions
149 lines (139 loc) · 2.71 KB
/
Password.cpp
File metadata and controls
149 lines (139 loc) · 2.71 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "Password.h"
#include <string>
using std::string;
/*
The function receives a string counts how many times the same character
occurs at the beginning of the string, before any other characters (or the
end of the string). The function is case-sensitive so 'Z' is different than
'z' and any ASCII characters are allowed.
*/
int Password::count_leading_characters(string phrase){
int repetition = 1;
int index = 0;
while( index < phrase.length()-1 && phrase[index] == phrase[index+1] ){
repetition++;
index++;
}
return repetition;
}
/*
receives a string and returns whether it has both at least one upper-case
letter and at least one lower-case letter
*/
// Implementation A
#include <cctype>
bool Password::has_mixed_case(string str)
{
bool has_lower = false;
bool has_upper = false;
for (char ch : str)
{
if (std::islower(ch))
{
has_lower = true;
}
else if (std::isupper(ch))
{
has_upper = true;
}
}
return has_lower && has_upper;
}
// Implementation B
/*
bool Password::has_mixed_case(string str)
{
int count_lower = 0;
for (int i = 0; i < str.length(); i++)
{
if (str[i] >= 'a' && str[i] <= 'z')
{
count_lower++;
}
}
return count_lower > 0 && count_lower < str.length();
}
*/
// Implementation C
/*
bool Password::has_mixed_case(string str)
{
bool lower = false;
bool upper = false;
for (int i = 0; i < str.length(); i++)
{
if (str[i] >= 'a' && str[i] <= 'z')
{
lower = true;
}
else if (str[i] >= 'A' && str[i] <= 'Z')
{
upper = true;
}
}
return upper && lower;
}
*/
// Implementation D
/*
#include <cctype>
bool Password::has_mixed_case(string str)
{
bool is_lower = false;
bool is_upper = false;
for (char c : str)
{
if (std::islower(c))
{
is_lower = true;
}
else
{
is_upper = true;
}
}
return is_lower && is_upper;
}
*/
// Implementation E
/*
bool Password::has_mixed_case(string str)
{
bool found = false;
for(char c : str){
if( !found && c >= 'A' && c <= 'Z' ){
found = true;
}
else if( found && c >= 'a' && c <= 'z'){
return true;
}
}
return false;
}
*/
unsigned int unique_characters(string s) {
int unique = 0;
vector<char> u;
bool isUnique;
if (s.length() == 0) {
return 0;
}
for (int i = 0; i < s.length(); i++) {
if (i == 0) {
u.push_back(s[0]);
unique++;
} else {
isUnique = true;
for (int j = 0; j < u.size(); j++) {
if (s[i] == u[j]) {
isUnique = false;
}
}
if (isUnique) {
u.push_back(s[i]);
unique++;
}
}
}
return unique;
}