Write a method that will have a C++ string passed to it. The string will be an e
ID: 3753234 • Letter: W
Question
Write a method that will have a C++ string passed to it. The string will be an email address and the method will return a bool to indicate that the email address is valid. Email Validation Rules: ( These rules are not official.) 1) No whitespace allowed 2) 1 and only 1 @ symbol 3) 1 and only 1 period allowed after the @ symbol. Periods can exist before the @ sign. 4) 3 and only 3 characters after the period is required. ***************************** Program will prompt for an email address and report if it is valid or not according to the rules posted above. The program will loop and prompt me to continue. ***************************** Use solutions from the functions described in Chapter 10.
Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
int main()
{
tryagain:
string str;
cout<<"========================"<<endl
cout<<"Enter Your Email address"<<endl;
cout<<"========================"<<endl
cin>>str;
int counts=0;
int countd=0
int valid=1;
for (int i=0;i<sizeof(str);i++)
{
if(isspace(str[i]))
{
cout<<endl;
cout<<"Invalid Email address"<<endl;
valid=0;
break;
}
if(str[i]=='@')
{
counts++;
if(counts>1)
{
cout<<"Invalid Email address"<<endl;
valid=0;
break;
}
}
if(str[i]=='@')
{
int j=i+1;
while(j<sizeof(str)-i)
{
if(str[j]=='.')
{
countd++;
if(countd>1)
{
cout<<"Invalid Email address"<<endl;
valid=0;
break;
}
}
j++;
}
}
if(str[i]=='@')
{
//int sum=0;
int j=i+1;
while(j<sizeof(str)-i)
{
if(str[j]=='.')
{
if(j+3!=sizeof(str)-1)
{
cout<<"Invalid Email address"<<endl;
valid=0;
break;
}
}
}
}
}
if(valid==1)
{
cout<<"Valid Email address"<<endl;
}
cout<<"Press 1 to try again or 0 to exit"<<endl;
char v;
if(v=='1')
{
goto tryagain;
}
else
{
exit(0);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.