You would like to set a password for a bank account. However, there are three restrictions on the format of the password:
- it has to contain only alphanumerical characters (a−z, A−Z, 0−9);
- there should be an even number of letters;
- there should be an odd number of digits.
You are given a string S consisting of N characters. String S can be divided into words by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.
For example, given "test 5 a0A pass007 ?xy1", there are five words and three of them are valid passwords: "5", "a0A" and "pass007". Thus the longest password is "pass007" and its length is 7. Note that neither "test" nor "?xy1" is a valid password, because "?" is not an alphanumerical character and "test" contains an even number of digits (zero).
Write a function:
int solution(char *S);
that, given a non-empty string S consisting of N characters, returns the length of the longest word from the string that is a valid password. If there is no such word, your function should return −1.
For example, given S = "test 5 a0A pass007 ?xy1", your function should return 7, as explained above.
Assume that:
- N is an integer within the range [1..200];
- string S consists only of printable ASCII characters and spaces.
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int isDigit(char c){
if((c<='9')&&(c>='0')) return 1;
return 0;
}
int isAlpha(char c){
if((c<='z')&&(c>='a')) return 1;
if((c<='Z')&&(c>='A')) return 1;
return 0;
}
int solution(char *S) {
// write your code in C99 (gcc 6.2.0)
int maxlen=-1;
int ndigit=0;
int nalpha=0;
if(S[0]==0) return -1;
int i=0;
int len=0;
while(1){
if(isDigit(S[i])){
//printf("%c is digit\n",S[i]);
ndigit++;
len++;
}
else if(isAlpha(S[i])){
//printf("%c is alpha\n",S[i]);
nalpha++;
len++;
}
else if((S[i]==' ')||(S[i]==0)){
//printf("[%d]len=%d ndigit=%d(%d), nalpha=%d(%d)\n",maxlen,len,ndigit,ndigit%2,nalpha,nalpha%2);
if((0==(nalpha%2))&&(ndigit%2)&&(len==(ndigit+nalpha))&&(maxlen<len)){
maxlen=len;
//printf("maxlen=%d\n",maxlen);
}
ndigit=0;
nalpha=0;
len=0;
}
if(S[i]==0)break;
i++;
}
return maxlen;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int isDigit(char c){
if((c<='9')&&(c>='0')) return 1;
return 0;
}
int isAlpha(char c){
if((c<='z')&&(c>='a')) return 1;
if((c<='Z')&&(c>='A')) return 1;
return 0;
}
int solution(char *S) {
// write your code in C99 (gcc 6.2.0)
int maxlen=-1;
int ndigit=0;
int nalpha=0;
if(S[0]==0) return -1;
int i=0;
int len=0;
while(1){
if(isDigit(S[i])){
//printf("%c is digit\n",S[i]);
ndigit++;
len++;
}
else if(isAlpha(S[i])){
//printf("%c is alpha\n",S[i]);
nalpha++;
len++;
}
else if((S[i]==' ')||(S[i]==0)){
//printf("[%d]len=%d ndigit=%d(%d), nalpha=%d(%d)\n",maxlen,len,ndigit,ndigit%2,nalpha,nalpha%2);
if((0==(nalpha%2))&&(ndigit%2)&&(len==(ndigit+nalpha))&&(maxlen<len)){
maxlen=len;
//printf("maxlen=%d\n",maxlen);
}
ndigit=0;
nalpha=0;
len=0;
}
if(S[i]==0)break;
i++;
}
return maxlen;
}
['zaq!2#edc']
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int isDigit(char c){
if((c<='9')&&(c>='0')) return 1;
return 0;
}
int isAlpha(char c){
if((c<='z')&&(c>='a')) return 1;
if((c<='Z')&&(c>='A')) return 1;
return 0;
}
int solution(char *S) {
// write your code in C99 (gcc 6.2.0)
int maxlen=-1;
int ndigit=0;
int nalpha=0;
if(S[0]==0) return -1;
int i=0;
int len=0;
while(1){
if(isDigit(S[i])){
printf("%c is digit\n",S[i]);
ndigit++;
len++;
}
else if(isAlpha(S[i])){
printf("%c is alpha\n",S[i]);
nalpha++;
len++;
}
else if((S[i]==' ')||(S[i]==0)){
printf("[%d]len=%d ndigit=%d(%d), nalpha=%d(%d)\n",maxlen,len,ndigit,ndigit%2,nalpha,nalpha%2);
if((0==(nalpha%2))&&(ndigit%2)&&(len==(ndigit+nalpha))&&(maxlen<len)){
maxlen=len;
printf("maxlen=%d\n",maxlen);
}
ndigit=0;
nalpha=0;
len=0;
}
if(S[i]==0)break;
i++;
}
return maxlen;
}
['zaq!2#edc']
t is alpha e is alpha s is alpha t is alpha [-1]len=4 ndigit=0(0), nalpha=4(0) 5 is digit [-1]len=1 ndigit=1(1), nalpha=0(0) maxlen=1 a is alpha 0 is digit A is alpha [1]len=3 ndigit=1(1), nalpha=2(0) maxlen=3 p is alpha a is alpha s is alpha s is alpha 0 is digit 0 is digit 7 is digit [3]len=7 ndigit=3(1), nalpha=4(0) maxlen=7 x is alpha y is alpha 1 is digit [7]len=3 ndigit=1(1), nalpha=2(0)
function result: 7
z is alpha a is alpha q is alpha 2 is digit e is alpha d is alpha c is alpha [-1]len=7 ndigit=1(1), nalpha=6(0) maxlen=7
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int isDigit(char c){
if((c<='9')&&(c>='0')) return 1;
return 0;
}
int isAlpha(char c){
if((c<='z')&&(c>='a')) return 1;
if((c<='Z')&&(c>='A')) return 1;
return 0;
}
int solution(char *S) {
// write your code in C99 (gcc 6.2.0)
int maxlen=-1;
int ndigit=0;
int nalpha=0;
if(S[0]==0) return -1;
int i=0;
int len=0;
while(1){
if(isDigit(S[i])){
printf("%c is digit\n",S[i]);
ndigit++;
}
else if(isAlpha(S[i])){
printf("%c is alpha\n",S[i]);
nalpha++;
len++;
}
else if((S[i]==' ')||(S[i]==0)){
printf("[%d]len=%d ndigit=%d(%d), nalpha=%d(%d)\n",maxlen,len,ndigit,ndigit%2,nalpha,nalpha%2);
if((0==(nalpha%2))&&(ndigit%2)&&(len==(ndigit+nalpha))&&(maxlen<len)){
maxlen=len;
printf("maxlen=%d\n",maxlen);
}
ndigit=0;
nalpha=0;
len=0;
}
if(S[i]==0)break;
i++;
}
return maxlen;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int isDigit(char c){
if((c<='9')&&(c>='0')) return 1;
return 0;
}
int isAlpha(char c){
if((c<='z')&&(c>='a')) return 1;
if((c<='Z')&&(c>='A')) return 1;
return 0;
}
int solution(char *S) {
// write your code in C99 (gcc 6.2.0)
int maxlen=-1;
int ndigit=0;
int nalpha=0;
if(S[0]==0) return -1;
int i=0;
int len=0;
while(1){
if(isDigit(S[i])){
printf("%c is digit\n",S[i]);
ndigit++;
}
else if(isAlpha(S[i])){
printf("%c is alpha\n",S[i]);
nalpha++;
}
else if((S[i]==' ')||(S[i]==0)){
printf("[%d]len=%d ndigit=%d(%d), nalpha=%d(%d)\n",maxlen,len,ndigit,ndigit%2,nalpha,nalpha%2);
if((0==(nalpha%2))&&(ndigit%2)&&(len==(ndigit+nalpha))&&(maxlen<len)){
maxlen=len;
printf("maxlen=%d\n",maxlen);
}
ndigit=0;
nalpha=0;
len=0;
}
if(S[i]==0)break;
i++;
}
return maxlen;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int isDigit(char c){
if((c<='9')&&(c>='0')) return 1;
return 0;
}
int isAlpha(char c){
if((c<='z')&&(c>='a')) return 1;
if((c<='Z')&&(c>='A')) return 1;
return 0;
}
int solution(char *S) {
// write your code in C99 (gcc 6.2.0)
int maxlen=-1;
int ndigit=0;
int nalpha=0;
if(S[0]==0) return -1;
int i=0;
int len=0;
while(1){
if(isDigit(S[i])){
printf("%c is digit\n",S[i]);
ndigit++;
}
else if(isAlpha(S[i])){
printf("%c is alpha\n",S[i]);
nalpha++;
}
else if((S[i]==' ')||(S[i]==0)){
printf("[%d]len=%d ndigit=%d(%d), nalpha=%d(%d)\n",maxlen,len,ndigit,ndigit%2,nalpha,nalpha%2);
if((0==(nalpha%2))&&(ndigit%2)&&(len==(ndigit+nalpha))&&(maxlen<len)){
maxlen=len;
printf("maxlen=%d\n",maxlen);
}
ndigit=0;
nalpha=0;
len=0;
}
if(S[i]==0)break;
i++;
len++;
}
return maxlen;
}
['zaq!2#edc']
t is alpha e is alpha s is alpha t is alpha [-1]len=4 ndigit=0(0), nalpha=4(0) 5 is digit [-1]len=2 ndigit=1(1), nalpha=0(0) a is alpha 0 is digit A is alpha [-1]len=4 ndigit=1(1), nalpha=2(0) p is alpha a is alpha s is alpha s is alpha 0 is digit 0 is digit 7 is digit [-1]len=8 ndigit=3(1), nalpha=4(0) x is alpha y is alpha 1 is digit [-1]len=5 ndigit=1(1), nalpha=2(0)
function result: -1
z is alpha a is alpha q is alpha 2 is digit e is alpha d is alpha c is alpha [-1]len=9 ndigit=1(1), nalpha=6(0)
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int isDigit(char c){
if((c<='9')&&(c>='0')) return 1;
return 0;
}
int isAlpha(char c){
if((c<='z')&&(c>='a')) return 1;
if((c<='Z')&&(c>='A')) return 1;
return 0;
}
int solution(char *S) {
// write your code in C99 (gcc 6.2.0)
int maxlen=-1;
int ndigit=0;
int nalpha=0;
if(S[0]==0) return -1;
int i=0;
int len=0;
while(1){
if(isDigit(S[i])){
printf("%c is digit\n",S[i]);
ndigit++;
}
else if(isAlpha(S[i])){
printf("%c is alpha\n",S[i]);
nalpha++;
}
else if((S[i]==' ')||(S[i]==0)){
printf("[%d]len=%d ndigit=%d(%d), nalpha=%d(%d)\n",maxlen,len,ndigit,ndigit%2,nalpha,nalpha%2);
if((0==(nalpha%2))&&(ndigit%2)&&(len==(ndigit+nalpha))&&(maxlen<len)){
maxlen=len;
printf("maxlen=%d\n",maxlen);
}
ndigit=0;
nalpha=0;
len=0;
}
if(S[i]==0)break;
i++;
}
return maxlen;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int isDigit(char c){
if((c<='9')&&(c>='0')) return 1;
return 0;
}
int isAlpha(char c){
if((c<='z')&&(c>='a')) return 1;
if((c<='Z')&&(c>='A')) return 1;
return 0;
}
int solution(char *S) {
// write your code in C99 (gcc 6.2.0)
int maxlen=-1;
int ndigit=0;
int nalpha=0;
if(S[0]==0) return -1;
int i=0;
int len=0;
while(1){
if(isDigit(S[i])){
printf("%c is digit\n",S[i]);
ndigit++;
len++;
}
else if(isAlpha(S[i])){
printf("%c is alpha\n",S[i]);
nalpha++;
len++;
}
else if((S[i]==' ')||(S[i]==0)){
printf("[%d]len=%d ndigit=%d(%d), nalpha=%d(%d)\n",maxlen,len,ndigit,ndigit%2,nalpha,nalpha%2);
if((0==(nalpha%2))&&(ndigit%2)&&(len==(ndigit+nalpha))&&(maxlen<len)){
maxlen=len;
printf("maxlen=%d\n",maxlen);
}
ndigit=0;
nalpha=0;
len=0;
}
else{
len++;
}
if(S[i]==0)break;
i++;
}
return maxlen;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int isDigit(char c){
if((c<='9')&&(c>='0')) return 1;
return 0;
}
int isAlpha(char c){
if((c<='z')&&(c>='a')) return 1;
if((c<='Z')&&(c>='A')) return 1;
return 0;
}
int solution(char *S) {
// write your code in C99 (gcc 6.2.0)
int maxlen=-1;
int ndigit=0;
int nalpha=0;
if(S[0]==0) return -1;
int i=0;
int len=0;
while(1){
if(isDigit(S[i])){
printf("%c is digit\n",S[i]);
ndigit++;
len++;
}
else if(isAlpha(S[i])){
printf("%c is alpha\n",S[i]);
nalpha++;
len++;
}
else if((S[i]==' ')||(S[i]==0)){
printf("[%d]len=%d ndigit=%d(%d), nalpha=%d(%d)\n",maxlen,len,ndigit,ndigit%2,nalpha,nalpha%2);
if((0==(nalpha%2))&&(ndigit%2)&&(len==(ndigit+nalpha))&&(maxlen<len)){
maxlen=len;
printf("maxlen=%d\n",maxlen);
}
ndigit=0;
nalpha=0;
len=0;
}
else{
printf("%c is invalid\n",S[i]);
len++;
}
if(S[i]==0)break;
i++;
}
return maxlen;
}
['zaq!2#edc']
t is alpha e is alpha s is alpha t is alpha [-1]len=4 ndigit=0(0), nalpha=4(0) 5 is digit [-1]len=1 ndigit=1(1), nalpha=0(0) maxlen=1 a is alpha 0 is digit A is alpha [1]len=3 ndigit=1(1), nalpha=2(0) maxlen=3 p is alpha a is alpha s is alpha s is alpha 0 is digit 0 is digit 7 is digit [3]len=7 ndigit=3(1), nalpha=4(0) maxlen=7 ? is invalid x is alpha y is alpha 1 is digit [7]len=4 ndigit=1(1), nalpha=2(0)
function result: -1
z is alpha a is alpha q is alpha ! is invalid 2 is digit # is invalid e is alpha d is alpha c is alpha [-1]len=9 ndigit=1(1), nalpha=6(0)
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int isDigit(char c){
if((c<='9')&&(c>='0')) return 1;
return 0;
}
int isAlpha(char c){
if((c<='z')&&(c>='a')) return 1;
if((c<='Z')&&(c>='A')) return 1;
return 0;
}
int solution(char *S) {
// write your code in C99 (gcc 6.2.0)
int maxlen=-1;
int ndigit=0;
int nalpha=0;
if(S[0]==0) return -1;
int i=0;
int len=0;
while(1){
if(isDigit(S[i])){
//printf("%c is digit\n",S[i]);
ndigit++;
len++;
}
else if(isAlpha(S[i])){
//printf("%c is alpha\n",S[i]);
nalpha++;
len++;
}
else if((S[i]==' ')||(S[i]==0)){
//printf("[%d]len=%d ndigit=%d(%d), nalpha=%d(%d)\n",maxlen,len,ndigit,ndigit%2,nalpha,nalpha%2);
if((0==(nalpha%2))&&(ndigit%2)&&(len==(ndigit+nalpha))&&(maxlen<len)){
maxlen=len;
//printf("maxlen=%d\n",maxlen);
}
ndigit=0;
nalpha=0;
len=0;
}
else{
//printf("%c is invalid\n",S[i]);
len++;
}
if(S[i]==0)break;
i++;
}
return maxlen;
}
['zaq!2#edc']
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int isDigit(char c){
if((c<='9')&&(c>='0')) return 1;
return 0;
}
int isAlpha(char c){
if((c<='z')&&(c>='a')) return 1;
if((c<='Z')&&(c>='A')) return 1;
return 0;
}
int solution(char *S) {
// write your code in C99 (gcc 6.2.0)
int maxlen=-1;
int ndigit=0;
int nalpha=0;
if(S[0]==0) return -1;
int i=0;
int len=0;
while(1){
if(isDigit(S[i])){
//printf("%c is digit\n",S[i]);
ndigit++;
len++;
}
else if(isAlpha(S[i])){
//printf("%c is alpha\n",S[i]);
nalpha++;
len++;
}
else if((S[i]==' ')||(S[i]==0)){
//printf("[%d]len=%d ndigit=%d(%d), nalpha=%d(%d)\n",maxlen,len,ndigit,ndigit%2,nalpha,nalpha%2);
if((0==(nalpha%2))&&(ndigit%2)&&(len==(ndigit+nalpha))&&(maxlen<len)){
maxlen=len;
//printf("maxlen=%d\n",maxlen);
}
ndigit=0;
nalpha=0;
len=0;
}
else{
//printf("%c is invalid\n",S[i]);
len++;
}
if(S[i]==0)break;
i++;
}
return maxlen;
}
['zaq!2#edc']
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int isDigit(char c){
if((c<='9')&&(c>='0')) return 1;
return 0;
}
int isAlpha(char c){
if((c<='z')&&(c>='a')) return 1;
if((c<='Z')&&(c>='A')) return 1;
return 0;
}
int solution(char *S) {
// write your code in C99 (gcc 6.2.0)
int maxlen=-1;
int ndigit=0;
int nalpha=0;
if(S[0]==0) return -1;
int i=0;
int len=0;
while(1){
if(isDigit(S[i])){
//printf("%c is digit\n",S[i]);
ndigit++;
len++;
}
else if(isAlpha(S[i])){
//printf("%c is alpha\n",S[i]);
nalpha++;
len++;
}
else if((S[i]==' ')||(S[i]==0)){
//printf("[%d]len=%d ndigit=%d(%d), nalpha=%d(%d)\n",maxlen,len,ndigit,ndigit%2,nalpha,nalpha%2);
if((0==(nalpha%2))&&(ndigit%2)&&(len==(ndigit+nalpha))&&(maxlen<len)){
maxlen=len;
//printf("maxlen=%d\n",maxlen);
}
ndigit=0;
nalpha=0;
len=0;
}
else{
//printf("%c is invalid\n",S[i]);
len++;
}
if(S[i]==0)break;
i++;
}
return maxlen;
}
The solution obtained perfect score.