How to validate an email in c

Asked

Viewed 1,303 times

0

Eai guys. I need to do an e-mail check with these features.

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

What I did was that, but he’s skipping a few 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");
    }
}
  • This is for some class or really need an efficient email validator?

  • yes it’s part of a job I need to deliver. I can’t use pointers or functions that do that in the case. If you know a better way to do thank you.

  • only c or can c++?

  • need in C. But in case you only know in c++ I can get some idea of how to do

2 answers

1


First, your program has some errors:

  1. The formatting is quite messy
  2. Don’t have the #includes
  3. Instead of if(email[i] > 3) should be if(i > 3)
  4. The line if(ponto=1 && email[i] >=2) you forgot to put == (ponto==1)
  5. Have some variables created and not used
  6. The code could be much simpler, in any case, one must follow its logic.

At this link (https://ideone.com/KuQFF3) you will be able to see your version without coding errors, but still it is not detecting correctly if the email is valid.

Below 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 in https://ideone.com/WdqZu1

  • Exactly, yet it doesn’t work very well. If you have any examples of code that I can look at why logic I understand, only maybe there’s a better way to solve this.

  • I edited my answer with a solution. If you agree, please ask the answer and accept.

  • if (arroba) break; in case the arroba is zero it?

  • 0 = FALSE, Not 0 = TRUE. if (arroba) is the same as if (arroba != 0).

0

How about using the function sscanf() of the standard 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;
}

Browser other questions tagged

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