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.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter9: Records (struct)
Section: Chapter Questions
Problem 2PE
icon
Related questions
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. Must not start with the @ symbol
  4. At least 1 period after the @ symbol.
  5. 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.

Expert Solution
Step 1

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

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
ADT and Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning