How to validate an email in c

0

Hey guys. I need to do an e-mail scan with these features.

Email must have: 3 characters or more; an at sign ("); other 3 characters or more; followed by an endpoint (.) signal; and a set of at least 2 characters.

What I did was this, but it is jumping some conditions.

main(){

    int x;
    char email[50]={"[email protected]"};
    int arroba,ponto,passa,maior,c_P,c_S,i;
    int tam=strlen(email);
    char teste='.';
     for (i = 0; i < tam; i++) {
       if(email[i] > 3){   
         maior=1;   
       }if(email[i] == '@'){
        arroba=1;

       }if(arroba == 1 &&  email[i] >= 3){
        c_P=1;
       }if(email[i]=='.'){
            ponto=1;
           }if(ponto=1 && email[i] >=2){
            c_S=1;
       }

    if(maior==1 && arroba == 1 && c_P==1 && c_S ==1){
        passa=1;
    }else{
        passa=0;
    }


    }  

    if(passa==1){
        printf("Valido");
    }else{
        printf("Invalido");
    }
}
    
asked by anonymous 27.06.2018 / 04:28

2 answers

1

First, your program has some errors:

  • Formatting is very messy
  • Do not have #includes
  • Instead of if(email[i] > 3) should be if(i > 3)
  • The line if(ponto=1 && email[i] >=2) you forgot to put == ( ponto==1 )
  • Has some created and unused variables
  • The code could be much simpler, in any case, one should follow its logic.
  • In this link ( link ) you can see your version without any encoding errors, but still not correctly detecting if the e- mail is valid.

    Below is a code I made in C that solves your problem:

    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
    
      char email[50]={"[email protected]"};
      int tam=strlen(email);
      int arroba = 0, ponto = 0, antesPonto = 0, depoisPonto = 0, i;
    
      for (i = 0; i < tam; i++) {
        char c = email[i];
        if(c == '@') {
          if (arroba)
            break; // não pode ter uma segunda @
          arroba = 1;
          if (i < 3)
            break; // se @ vier antes de 3 caracteres, erro
        }
        else if (arroba) { // se já encontrou @
          if (ponto) { // se já encontrou . depois de @
            depoisPonto++;
          }
          else if(c == '.') {
            ponto = 1;
            if (antesPonto < 3) {
              break; // se . depois de @ vier antes de 3 caracteres, erro
            }
          }
          else {
            antesPonto++;
          }
        }
      } // for
    
      if (i == tam && depoisPonto > 1)
        printf("Valido");
      else
        printf("Invalido");
    }
    

    See working at link

        
    27.06.2018 / 05:01
    0

    How about using the sscanf() function of the default library stdio.h ?

    #include <stdio.h>
    
    int validar_email( const char * email )
    {
        char usuario[256], site[256], dominio[256];
    
        if( sscanf( email, "%[^@ \t\n]@%[^. \t\n].%3[^ \t\n]", usuario, site, dominio ) != 3 )
            return 0;
    
        return 1;
    }
    
    int main( int argc, char ** argv )
    {
        if( !validar_email(argv[1]) )
        {
            printf("Invalido!\n");
            return 1;
        }
    
        printf("OK!\n");
        return 0;
    }
    
        
    28.06.2018 / 23:50