Check if given email address is valid or not in C++
Last Updated :
26 Oct, 2020
Given a string email that denotes an Email Address, the task is to check if the given string is a valid email id or not. If found to be true, then print “Valid”. Otherwise, print “Invalid”. A valid email address consists of an email prefix and an email domain, both in acceptable formats:
- The email address must start with a letter (no numbers or symbols).
- There must be an @ somewhere in the string that is located before the dot.
- There must be text after the @ symbol but before the dot.
- There must be a dot and text after the dot.
Examples:
Input: email = “review-team@geeksforgeeks.org”
Output: Valid
Explanation:
The given string follow all the criteria for an valid email string.
Input: email = “contribute@geeksforgeeks..org”
Output: Invalid
String Traversal based Approach: Follow the steps below:
- Check if the first character of the email id string is an alphabet or not. If not, then the email is Invalid.
- Now traverse over the string email to find the position the “@” and “.” If “@” or “.” is not present then the email is Invalid.
- If “.” is not present after “@” then the email is Invalid.
- If “.” is the last character of the string email then the email id is Invalid.
- Otherwise, the email is Valid.
Below is the implementation of the above approach:
C++
#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 = "review-team@geeksforgeeks.org" ;
bool ans = is_valid(email);
if (ans) {
cout << email << " : "
<< "valid" << endl;
}
else {
cout << email << " : "
<< "invalid" << endl;
}
return 0;
}
|
Output:
review-team@geeksforgeeks.org : valid
Time Complexity: O(N)
Auxiliary Space: O(1)
Regular Expression based Approach: The given problem can also be solved using Regular Expression. Below are the steps:
- Get the email string.
- Create a regular expression to check the valid email as mentioned below:
regex = “(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+”
- Match the given string email with the regular expression. In C++, this can be done by using regex_match().
- Print “Valid” if the given string email matches with the given regular expression, else return “Invalid”.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <regex>
using namespace std;
bool isValid( const string& email)
{
const regex pattern(
"(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+" );
return regex_match(email, pattern);
}
int main()
{
string email
= "review-team@geeksforgeeks.org" ;
bool ans = isValid(email);
if (ans) {
cout << email << " : "
<< "valid" << endl;
}
else {
cout << email << " : "
<< "invalid" << endl;
}
}
|
Output:
review-team@geeksforgeeks.org : valid
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...