Program must be written in c++ !!! Write a function called is_valid_email that t
ID: 668744 • Letter: P
Question
Program must be written in c++ !!!
Write a function called is_valid_email that takes an array of characters representing an email id, the array?s size, and returns true if it is a valid email id, else false. An email id is valid if it contains a sequence of characters followed by ?@?, followed by another sequence of characters, and ends with .com. Assume that the array only contains alphabets, and special characters ?@? and ?.?. Do not use in built string library functionalities. Using a main function, demonstrate that it works.Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
char mail[100]={0};
int count,i=0,j;
bool at_flag=false;
bool special_char_flag=false;
bool com_flag=false;
bool is_valid_email(char mail[],int count){
//checking for given input to end at .com
if(mail[count-4]=='.' && mail[count-3]=='c' && mail[count-2]=='o' && mail[count-1]=='m')
com_flag=true;
for(j=0;j<=i-1;j++)
{
if(mail[j]=='@' && j!=0){
at_flag=true; //check if given input contains @ after some seq of chars
}
if((mail[j]>='a' && mail[j]<='z') ||(mail[j]>='A' && mail[j]<='Z') || (mail[j]>='0' && mail[j] <= '9' ) ||mail[j]=='.' || mail[j]=='@')
special_char_flag=false;
else
{ special_char_flag=true; //check if given input contains any special characterss
break;
}
}
if(at_flag && com_flag && !special_char_flag)
return true;
else
return false;
}
int main(){
cout<<"Enter the email id ";
while (cin.peek() != ' ') {
cin >> mail[i++];}
cout<<" size of entered mail id is "<<i;
count=i;
bool check=is_valid_email(mail,count);
if(check)
cout<<" It is a valid email id ";
else
cout<<" It is an invalid email id ";
getch();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.