Write a program to input a mail_id . Check the validity of the input given as per the following rules : -
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]
#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;
}
Comments
Leave a comment