C Password Validator

Asked

Viewed 826 times

1

I’m trying to solve the problem 2253 - Password Validator, URI, but is giving 10% wrong answer, but all my tests are working, someone can 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;
}

2 answers

2


The biggest problem is this while that doesn’t make sense. I improved some other things that might give 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");
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • The problem requires the while because the entry of the dreaded problem with end of file, but thanks anyway.

  • But it makes no sense, because this function does not return a file end but a pointer.

  • @Lettuce, you can ask her yourself stdin if she’s already finished: feof(stdin)

  • Or check in the documentation that he returns NULL when it reaches the end of the file and cannot read even 1 character

  • I switched to NULL and now does not exceed the time limit, but there is 10% wrong answer :/

  • There’s no point in asking EOF right in the fgets with stdin. When he switched to NULL essentially made the while not be necessary more, then do the same as my answer, only mine has no unnecessary instructions. The goal of these exercises is to make learn and not be able to solve them and not understand what is there.

  • I understand what’s there, but it’s that I really need to figure out how it’s there, it’s a practical job.

  • Well, only you know what’s there, and if that’s what you’re talking about, it asks you to do something wrong, or you’re interpreting something wrong. It is difficult to help more than this given what was past. The question did not speak of this, so what was asked has already been answered.

  • Lettuce, it would be easier to explain if you had placed a link ;-) Maniero, the lettuce wants to answer this test:https://www.urionlinejudge.com.br/judge/pt/problems/view/2253. Its code will be validated by a file containing several lines, so the while'.

  • Thank you all, I solved the issue, I just switched from C to C++ and it worked.

Show 5 more comments

2

You can try using a Regex pattern for this:

#include <regex.h>

And for example use as 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.