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 = "[email protected]"
Output: Valid
Explanation:
The given string follow all the criteria for an valid email string.
Input: email = "[email protected]"
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check the character
// is an alphabet or not
bool isChar(char c)
{
return ((c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z'));
}
// Function to check the character
// is an digit or not
bool isDigit(const char c)
{
return (c >= '0' && c <= '9');
}
// Function to check email id is
// valid or not
bool is_valid(string email)
{
// Check the first character
// is an alphabet or not
if (!isChar(email[0])) {
// If it's not an alphabet
// email id is not valid
return 0;
}
// Variable to store position
// of At and Dot
int At = -1, Dot = -1;
// Traverse over the email id
// string to find position of
// Dot and At
for (int i = 0;
i < email.length(); i++) {
// If the character is '@'
if (email[i] == '@') {
At = i;
}
// If character is '.'
else if (email[i] == '.') {
Dot = i;
}
}
// If At or Dot is not present
if (At == -1 || Dot == -1)
return 0;
// If Dot is present before At
if (At > Dot)
return 0;
// If Dot is present at the end
return !(Dot >= (email.length() - 1));
}
// Driver Code
int main()
{
// Given string email
string email = "[email protected]";
// Function Call
bool ans = is_valid(email);
// Print the result
if (ans) {
cout << email << " : "
<< "valid" << endl;
}
else {
cout << email << " : "
<< "invalid" << endl;
}
return 0;
}
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++
// C++ program for the above approach
#include <iostream>
#include <regex>
using namespace std;
// Function to check the email id
// is valid or not
bool isValid(const string& email)
{
// Regular expression definition
const regex pattern(
"(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");
// Match the string pattern
// with regular expression
return regex_match(email, pattern);
}
// Driver Code
int main()
{
// Given string email
string email
= "[email protected]";
// Function Call
bool ans = isValid(email);
// Print the result
if (ans) {
cout << email << " : "
<< "valid" << endl;
}
else {
cout << email << " : "
<< "invalid" << endl;
}
}
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Check if a given string is a valid Hexadecimal Color Code or not Given a string str, the task is to check whether the given string is an HTML Hex Color Code or not. Print Yes if it is, otherwise print No. Examples: Input: str = â#1AFFa1âOutput: Yes Input: str = â#F00âOutput: Yes Input: str = �âOutput: No Approach: An HTML Hex Color Code follows the below-mentione
7 min read
Program to check if a date is valid or not Given a date, check if it is valid or not. It may be assumed that the given date is in range from 01/01/1800 to 31/12/9999. Examples : Input : d = 10, m = 12, y = 2000 Output : Yes The given date 10/12/2000 is valid Input : d = 30, m = 2, y = 2000 Output : No The given date 30/2/2000 is invalid. The
8 min read
Check if URL is valid or not in Java Given a URL as string, we need to find if the given URL is valid or not. Input : str = "https://www.geeksforgeeks.org/" Output : Yes Input : str = "https:// www.geeksforgeeks.org/" Output : No Note that there is a space after https:// Using java.net.url We can use java.net.url class to validate a UR
1 min read
Check if a given string is a valid number (Integer or Floating Point) | SET 1(Basic approach) Validate if a given string is numeric. Examples: Input : str = "11.5" Output : true Input : str = "abc" Output : false Input : str = "2e10" Output : true Input : 10e5.4 Output : false The following cases need to be handled in the code. Ignore the leading and trailing white spaces.Ignore the '+', '-'
11 min read
Check if an URL is valid or not using Regular Expression Given a URL as a character string str of size N.The task is to check if the given URL is valid or not.Examples : Input : str = "https://www.geeksforgeeks.org/" Output : Yes Explanation : The above URL is a valid URL.Input : str = "https:// www.geeksforgeeks.org/" Output : No Explanation : Note that
4 min read
Check whether the given string is a valid identifier Given a string str, the task is to check if the string is a valid identifier or not. In order to qualify as a valid identifier, the string must satisfy the following conditions: It must start with an either underscore(_) or any of the characters from the ranges ['a', 'z'] and ['A', 'Z'].There must n
6 min read