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

validate_email.cpp

#include<iostream>
using namespace std;

bool validate(string email)
{
   int i;
   for(i=0; i<email.length();i++)
   {
       // If white space found return false
       if(email[i]==' ')
       {
           return false;
       }
       // If @ found the break the loop
       if(email[i]=='@')
       {
           i++;
           break;
       }
   }
   for(; i<email.length();i++)
   {
       // If another @ or a white space found the return false
       if(email[i]=='@' || email[i]==' ')
       {
           return false;
       }
       // If period found then break the loop
       if(email[i]=='.')
       {
           i++;
           break;
       }
   }
   // If number of characters after period is not 3 then return false
   if(email.length()-i!=3)
   {
       return false;
   }
   for(; i<email.length(); i++)
   {
       // If another @ or period or a white space found then return false
       if(email[i]=='@' || email[i]=='.' || email[i]==' ')
       {
           return false;
       }
   }
   // Execution comes here if email address is valid
   return true;
}

int main()
{
   string email;
   cout<<"Enter email address: ";
   // User input
   getline(cin,email);
   // Function calling and output printing
   validate(email)==1?cout<<"Valid":cout<<"Invalid";
   return 0;
}

output screenshot:


Related Solutions

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...
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...
Write a method in C# which takes string as an argument and extracts all words of...
Write a method in C# which takes string as an argument and extracts all words of length between 4 to 5 and contains vowels in it. Method returns an array of type string containing words which satisfied above criteria. Show these words in main().
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 the method: public static String removeSubstring(String s1, String s2) Method removeSubstring(String s1, String s2) Must...
Write the method: public static String removeSubstring(String s1, String s2) Method removeSubstring(String s1, String s2) Must do 3 things: 1. Take two parameters, the original String (String s1) and the substring (String s2) that is to be removed; 2. Create a new String that consists of the original String data less all occurrences of the substring data; 3. Return the new String. Note: 1. The data to be removed is each occurrence of the complete substring, not individual characters of...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT