email validation in c/c++ without using regular expressions

Dec 11, 2013 at 10:58am
Hi,

Can anyone help me for writing a program for "Validating the e-mail id without using regular expressions in c/c++"?
Last edited on Dec 11, 2013 at 10:58am
Dec 11, 2013 at 12:32pm
A traditional valid email address can be of the form:

john_smith@panda-museum.co.uk

1) search for the @ character, does it appear only once? if yes:
2) search characters on the left of @: are they from the valid set? if yes:
3) search characters to the right of @: are they from the valid set? if yes:
4) does the character . appear at least once to the right of @? if yes:
5) the email address is valid.

constexpr std::string valid_set("0123456789abcdefghijklmnopqrstuvwxyz._-");
Dec 24, 2013 at 8:40am
Hi,

Anyone provide code for email validation without using any regular expressions in c/c++.

I tried but there is no luck.

Please help me.
Dec 24, 2013 at 10:00am
I tried but there is no luck.

Well there's no luck involved in knowing how to write a program.
Otherwise we could as well be monkeys punching keyboards.

Post your current code, and I'll try to help further.
Dec 26, 2013 at 2:49am
HI,

Please verify it. I hope this is not a complete picture for validating, need more additional checks.

#include <iostream>

using namespace std;

bool isvalidchar(char);
int main()
{
string s;
int i,offset1,offset2,n;
bool charflag=true,offsetflag=false;

cout << "enter the email id" << endl;
cin>>s;
cout << "the email id" << s<<endl;

n=s.length();

for(i=0;i<n;i++)
{
if(!isvalidchar(s[i]))
{
cout<<"inside is not valid";
charflag=false;
break;
}
}

for(i=0;i<n;i++)
{
if(s[i]=='@')
{
offset1=i;
cout<<"offset1"<<offset1<<endl;
}
if(s[i]=='.')
{
offset2=i;
cout<<"offset2"<<offset2<<endl;
}
}

if(offset1<offset2)
{
offsetflag=true;
cout<<"inside1"<<endl;
}

if((offsetflag==true)&&(charflag==true))
{
cout<<"Is a valid email id"<<endl;
}
else
{
cout<<"Is not a valid email id"<<endl;
}



return 0;
}

bool isvalidchar(char c)
{
bool result = false;
if (c >='A' && c <= 'Z' || c >= 'a' && c <= 'z'|| c >= '0' && c <='9' ||c =='.'||c =='-' || c =='+'|| c =='@'|| c =='_') result = true;
return result;
}

Please help me on this.
Last edited on Dec 26, 2013 at 3:23am
Topic archived. No new replies allowed.