In: Computer Science
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.
*****************************
Please find the running C++ code for given problem. Please do comments in case of any issue or more information is needed. Also, if you like my work please give it a thumbs up to encourage. Thank You.
The logic in the below C++ program has been implemented as described in the problem. The program asks user to enter an email address then the program checks the email validity as per the rules prescribed in the problem and display the result.
The program counts the the number of period, whitespace , @ and number of character after period character. Based on the counting it takes the decision. The inline suitable comments have also been added in the code for your better understanding.
You can use any C++ compiler to run below file. The output screenshot has also been attached for your reference.
Explanation:
C++ Source Code:
#include<iostream>//for taking input from user and print
#include <string>//for string operations
using namespace std;
//isValidEmail() takes user entered email address as argument
//returns true if email is valid else returns false if not valid
bool isValidEmail(string email)
{
int whiteSpaceCount = 0; //whiteSpaceCount stores count of whiteSpace in email
int periodCount = 0;//whiteSpaceCount stores count of period in email
int characterCount = 0;//whiteSpaceCount stores count of character after period in email
int atCount = 0;//whiteSpaceCount stores count of @ count in email
int periodIndex; //periodIndex stores the index of period character in email
int atIndex;//atIndex stores index of @ character in email
//Logic
//Now we will traverse the entered email and count the whiteSpaceCount and atCount. If whiteSpaceCount > 0 or atCount is //not one then email is not valid and we will return false.We will also find the index of period and period for applying rule //for them further in the code
for (int index = 0; index < email.length(); index++) // Error
{
if (email[index] == ' ') //if character is whitespace
whiteSpaceCount++; //increase whiteSpaceCount
if (email[index] == '@')//if character is @
{
atIndex=index;//atIndex stores index of @ character in email
atCount++; //increase atCount
}
if (email[index] == '.')
{
periodIndex=index;//periodIndex stores the index of period character in email
}
}
//if whiteSpaceCount and atCount does not staisfy the prescribed rules then email address is not valid and return false and exit
if (whiteSpaceCount > 0 || atCount != 1)
return false;
//else if whiteSpaceCount and atCount staisfy the prescribed rules now checking rules for period and character after period
else
{
//start from @ index counting the number of periods
for (int index = atIndex + 1; index < email.length(); index++) // Error
{
if (email[index] == '.')//if period is found
periodCount++;//increase the periodCount
}
//if periodCount not 1 then email address is invalid return false and exit
if (periodCount != 1)
return false;
//If periodCount is 1 then checking rule for character after period
else
{ //start from period index counting the number of character
for (int index = periodIndex + 1; index < email.length(); index++) //Error
{
characterCount++;//increasing the characterCount
}
//As mentiond in the Rules if characterCount is 3 after period the email address is valid so return true and exit
if (characterCount == 3)
return true;
else//otherwise email not valid
return false;
}
}
}
//Driver method
int main()
{
string email; //to hold the emailAddress entered by user
//boolean variable which will hold true if email is valid
//and false if not valid as per defind rules
bool valid;
while (true) //infinite loop
{
//prompt on the console for entering the email address
cout << "Please enter an email address for checking its validity as per rules" << endl;
getline(cin, email); //read email from console
valid=isValidEmail(email); //calling isValidEmail()
if (valid) //if value is true
cout << "This email address is valid"<<endl;
else //if value is false
cout << "This email address is not valid"<<endl;
}
return 0;
}
Output:
When the above code is run and user email address are provided by user from the console then the sample output is generated as below-