Answer to Question #268094 in C++ for gikovi

Question #268094

Write a program to input a mail_id . Check the validity of the input given as per the following rules : -

  • The mail should contain the character '@' anywhere in the string, expect for the first character.
  • The mail id should end with ".iit.ac.in"

If the entered string is not valid, throw an exception and display appropriate message. The string input should be taken in main. The exception is thrown in a function called from main. Exception handling code should be in main as well as in the called function. [ Hint: Rethrow the exception]


1
Expert's answer
2021-11-18T15:20:01-0500
#include <iostream>
#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){
if(!isChar(email[0])){
    return 0;
}
int At=-1,Dot=-1;
for(int i=0;i<email.length();i++){
    if(email[i]=='@'){
        At=i;
    }
    else if(email[i]=='.'){
        Dot=i;
    }
}
if (At==-1||Dot==-1)
    return 0;
if (At>Dot)
    return 0;
return !(Dot>=(email.length()-1));
}
int main()
{
    string email;
    cout << "Enter the email address" << endl;
    cin>>email;
    bool ans= is_valid(email);
    if (ans){
         if (ans){
        cout<<email<<" : "<<"email valid"<<endl;
    }
    }
    else{
      cout<<email<<" : "<<" email invalid"<<endl;
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS