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.) No whitespace allowed 1 and only 1 @ symbol Must not start with the @ symbol At least 1 period after the @ symbol. Must not start with a period after the @ symbol 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.
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.)
- No whitespace allowed
- 1 and only 1 @ symbol
- Must not start with the @ symbol
- At least 1 period after the @ symbol.
- Must not start with a period after the @ symbol
C++ Program Code:
#include <bits/stdc++.h>
using namespace std;
bool isChar(char c)
{
return ((c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z'));
}
bool isDigit(const char c)
{
return (c >= '0' && c <= '9');
}
bool is_valid(string email)
{
int k=0;
if (!isChar(email[0])) {
return 0;
}
int At = -1, Dot = -1;
for (int i = 0;
i < email.length(); i++) {
if (email[i] == '@') {
At = i;
k++;
}
else if (email[i] == '.') {
Dot = i;
}
}
if (At == -1 || Dot == -1 )
return 0;
if (At > Dot)
return 0;
if(k>1)
return 0;
return !(Dot >= (email.length() - 1));
}
int main()
{
string email;
cout<<"Enter an email: ";
cin>>email;
bool ans = is_valid(email);
if (ans) {
cout << email << " : "
<< "valid" << endl;
}
else {
cout << email << " : "
<< "invalid" << endl;
}
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images