Question

In: Computer Science

Write a method that will have a C++ string passed to it. The string will be...

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.

*****************************

Solutions

Expert Solution

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-


Related Solutions

Part 1 readFile(String filename) In this method you are passed a String with the name of...
Part 1 readFile(String filename) In this method you are passed a String with the name of a file. This method will read the file in line by line and store each line in a String array. This String array is then returned. An example is shown below. File Contents: Purple Rain by Prince I never meant to cause you any sorrow I never meant to cause you any pain I only wanted one time to see you laughing I only...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings as parameters and returns true if the two strings contain the same sequence of characters as each other but in the opposite order and false otherwise. • The recursive function should ignore capitalization. (For example, the call of isReverse("hello", "eLLoH") would return true.) • The empty string, as well as any one letter string, should be its own reverse. Write a driver program that...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { //...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str). May an array of...
Write a method ( C++ ) map occurance_map(const string path); that reads in an ascii text...
Write a method ( C++ ) map occurance_map(const string path); that reads in an ascii text file and returns an assocation where each key is a word in the text file and each value is the number of occurances of that word. Ignore punctuation and numbers. The method should be case-insensitive and should store the keys as lowercase. A word is definited by an string consisting entirely of alpha-numeric characters or apostrophes (single quote characteris). For example, if the file...
6. Write a Python function that checks whether a passed string is palindrome or not. Note:-A...
6. Write a Python function that checks whether a passed string is palindrome or not. Note:-A palindrome is a word, phrase, or sequence that reads the same backward as forward . Some examples you may try: “madam” redder “race car” Eva, Can I Stab Bats In A Cave? If the argument passed is not a string, invoke an exception or an assertion and state in a comment which one you have chosen and why.
Write a method which is passed A[], which is an array of int, and an int...
Write a method which is passed A[], which is an array of int, and an int passingScore. The method returns the number of items in A[] which are greater than or equal to passingScore. Write a method which is passed an array of int A[]. The method returns true if A[] is the same backwards and forwards. Write a method same( ), which is passed two arrays of int. The method returns true if the two arrays contain exactly the...
Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
write a program in C Write a function that is passed an array of characters containing...
write a program in C Write a function that is passed an array of characters containing letter grades of A, B, C, D, and F, and returns the total number of occurrences of each letter grade. Your function should accept both lower and upper case grades, for example, both 'b' and 'B' should be bucketed into your running total for B grades. Any grade that is invalid should be bucketed as a grade of 'I' for Incomplete. You must use...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns an ArrayList in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the ArrayList and then reverse the...
Write a function void reverse(char * s) that reverses the string passed as an argument. Your...
Write a function void reverse(char * s) that reverses the string passed as an argument. Your code should use pointer arithmetic (it may increment and decrement pointers, but it may not use array indexing). Here is a piece of code that shows the behavior of reverse: char buf[100]; strcpy(buf, “hello”); reverse(buf); printf(“%s\n”, buf); // output should be olleh
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT