Password Validator in C [closed]

1

I'm trying to solve the problem 2253 - Password Validator , but it is giving 10% wrong answer, but all my tests are working, can anyone find the error?

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int validade(char *S) {
    int i, maiuscula = 0, minuscula = 0, numero = 0, tam = strlen(S) - 1;
    if(tam < 6 || tam > 32)
        return 0;
    for(i = 0; i < tam; i++) {
        if(isupper(S[i]))
            maiuscula = 1;
        else if(islower(S[i]))
            minuscula = 1;
        else if(isdigit(S[i]))
            numero = 1;
        else
            return 0;
    }
    return maiuscula * minuscula * numero;

}
int main() {
    char S[100];

    while(fgets(S, 100, stdin) != NULL)
        printf(validade(S) ? "Senha valida.\n" : "Senha invalida.\n");

    return 0;
}
    
asked by anonymous 24.09.2017 / 03:08

2 answers

2

The biggest problem is this while which does not make sense. I've improved some other things that can make some gain.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int validade(char *S) {
    int tamanho = strlen(S);
    if (tamanho <= 6 || tamanho >= 32) return 0;
    int maiuscula = 0, minuscula = 0, numero = 0;
    for (int i = 0; i < tamanho - 1; i++) {
        if (islower(S[i])) maiuscula = 1;
        else if(isupper(S[i])) minuscula = 1;
        else if(isdigit(S[i])) numero = 1;
        else return 0;
    }
    return maiuscula * minuscula * numero;
}

int main() {
    char S[40];
    fgets(S, 40, stdin);
    printf(validade(S) ? "Senha valida.\n" : "Senha invalida.\n");
}

See running on ideone . And in Coding Ground . Also put it on GitHub for future reference .

    
24.09.2017 / 03:25
2

You can try to use a RegEx pattern for this:

#include <regex.h>

And for example default:

^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{4,}$

Explanation:

^      inicio da palavra
(?=.*) "lookahead" procura para a frente
\d     numero
[a-z]  letra minuscula
[A-Z]  letra maiuscula
{4,}   pelo menos quatro caracteres
$      fim da palavra
    
24.09.2017 / 03:53