-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestPassword.cpp
More file actions
49 lines (44 loc) · 1.27 KB
/
LongestPassword.cpp
File metadata and controls
49 lines (44 loc) · 1.27 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
// you can use includes, for example:
// #include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
std::istringstream iss(S);
int longest_password = 0;
do
{
std::string sub;
iss >> sub;
//std::cout << "Substring: " << sub << std::endl;
int countNumbers = 0;
int countLetters = 0;
for ( size_t i = 0; i < sub.size(); i++ )
{
if( isdigit(sub[i]))
{ countNumbers++; }
if( isalpha(sub[i]))
{ countLetters++; }
}
//check for symbols
if(countNumbers + countLetters == sub.size())
{
//check for odd number of digits
if((countNumbers % 2) != 0)
{
//check for even number of letters
if((countLetters % 2) == 0)
{
if(sub.size() > longest_password)
{
longest_password = sub.size();
}
}
}
}
} while (iss);
return longest_password;
}