title: Validate email address using function and call it in the main
body: <p>I'm trying to write a function that check e-mail...using simple tests.
  I wrote this code but I don't know what's wrong with it.
  All that I need in the validation:</p>
  
  <ul>
  <li>One or more characters, followed by '@', followed by one or more characters, followed by '.', followed by one more more characters.</li>
  <li>Must not contain spaces.</li>
  <li>Must be unique!</li>
  </ul>
  
  <p>Code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  int valid_mail(char x[]);
  int main()
  {
       do{
  char mail[100];
  gets(mail);
  }while(valid_mail(mail)==-1);
    return 0;
  }
  
  int valid_mail(char x[])
      {
     int count=0;
  int countp=0;
  int i,j;
  int t,t2;
  int k=0;
  for(i=0 ; i&lt;strlen(x) ; i++)
  {
      if(x[i]=='@')
      {
          count++;
          t=i;
      }
  }
  if(count==1)
  {
      for(j=0; j&lt;t ; j++)
      {
          if(!(x[j]=='_' || x[j]=='.' || isalpha(x[j]) || isdigit(x[j])))
          {
              k=-1;
              printf("\nInvalid e-mail\n");
              break;
          }
      }
      if(k==0)
      {
          for(i=0; i&gt;t ; i++)
          {
              if(x[i]=='.')
              {
                  t2=i;
                  countp++;
              }
          }
          if(countp==1)
          {
              for(i=0; i&lt;t2 &amp;&amp; i&gt;t2; i++)
              {
                  if(!(isalpha(x[i])))
                  {
                      k=-1;
                      printf("\nInvalid e-mail\n");
                      break;
                  }
                  else
                  {
                      k=1;
                  }
              }
              if(k==1)
              {
                  if(x[t2+3]!='\0')
                  {
                      k=-1;
                      printf("\nInvalid e-mail\n");
                  }
  
              }
          }
          else
          {
              k=-1;
              printf("\nInvalid e-mail\n");
          }
      }
  }
  else
  {
      k=-1;
      printf("\nInvalid e-mail\n");
  }
  return k;
  
  }
  </code></pre>