-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathstring-validation.cpp
74 lines (67 loc) · 1.88 KB
/
string-validation.cpp
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
// https://practice.geeksforgeeks.org/problems/string-validation/1/?track=DSA-Foundation-Final-String&batchId=193
/*
Given a string s representing a password, you need to check if the string is
valid or not.
The string must:
- have the length greater than or equal to 10.
- contain at least 1 numeric character.
- contain at least 1 uppercase character.
- contain at least 1 lowercase character.
- contain at least 1 special character from @#$-*.
*/
bool isThereAnUpperCase(string word)
{
for(int i=0; i < word.size(); i++)
{
if(isupper(word[i]))
return true;
}
return false;
}
bool isThereALowerCase(string word)
{
for(int i =0; i < word.size(); i++)
{
if(islower(word[i]))
return true;
}
return false;
}
bool isThereANumeric(string word)
{
for(int i =0; i < word.size(); i++)
{
if(isdigit(word[i]))
return true;
}
return false;
}
bool is_there_a_special_char(string word)
{
for(int i =0; i < word.size(); i++)
{
if(word[i] == '@' || word[i] == '#' || word[i] == '$' || word[i] == '-' || word[i] == '*' || word[i] == '.')
return true;
}
return false;
}
bool validate(string s)
{
// must have the length greater than or equal to 10
if(s.size() < 10)
return false;
// Check if the string contain at least 1 uppercase character
else if(!isThereAnUpperCase(s))
return false;
// Check if the string contain at least 1 lowercase character.
else if(!isThereALowerCase(s))
return false;
// CHeck if the string contains at least 1 number
else if(!isThereANumeric(s))
return false;
// check if must contain at least 1 special character from @#$-*.
else if(!is_there_a_special_char(s))
return false;
// All the conditions has been satisfied
return true;
}