Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created April 26, 2015 13:15
Show Gist options
  • Save krysseltillada/6162664fb297a8327fc7 to your computer and use it in GitHub Desktop.
Save krysseltillada/6162664fb297a8327fc7 to your computer and use it in GitHub Desktop.
email address validator
#include <string>
#include <iostream>
using namespace std;
bool chck_email(string);
int main()
{
string str;
cout << "enter an email address: ";
getline(cin, str);
if(chck_email(str))
cout << "this is an email address" << endl;
else
cout << "this is not an email address" << endl;
return 0;
}
bool chck_email(string chck)
{
string getdomain;
int countstring1 = 0, countstring2 = 0, countstring3 = 0;
for(int c = 0; c < chck.length(); c++)
{
if(chck.at(c) == '@'){
countstring1++;
countstring3++;
for(; countstring1 < chck.length(); countstring1++){
if(chck.at(countstring1) == '.'){
countstring2++;
countstring2 = countstring2 + countstring3;
getdomain = chck.substr(countstring2, int(chck.length()));
if(getdomain == "com" || getdomain == "org" || getdomain == "net" || getdomain == "int" || getdomain == "edu" || getdomain == "gov" || getdomain == "mil"){
return true;
}
else{
return false;
}
}
else{
countstring2++;
continue;
}
}
}
else{
countstring1++;
countstring3++;
continue;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment