Created
March 5, 2018 19:14
-
-
Save eduardoaugustojulio/6ad24f3f74f656ebfac251d9ddf9345d to your computer and use it in GitHub Desktop.
check if e-mail passed as argument is valid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <algorithm> | |
bool is_digits(const std::string &s) | |
{ | |
return std::any_of(s.begin(), s.end(), ::isdigit); | |
} | |
bool is_all_lower(const std::string &s) | |
{ | |
return std::all_of(s.begin(), s.end(), ::islower); | |
} | |
std::string isValid(std::string email) | |
{ | |
std::size_t first_delimeter = email.find("@"); | |
std::size_t second_delimeter = email.find("."); | |
if(first_delimeter != std::string::npos && second_delimeter != std::string::npos) | |
{ | |
std::string name = email.substr(0, first_delimeter); | |
std::string provider = email.substr(first_delimeter + 1, (second_delimeter - first_delimeter) - 1); | |
std::string adress = email.substr(second_delimeter + 1); | |
if(name.size() == 5 && !is_digits(name) && is_all_lower(name)){ | |
if(provider == "hogwarts" && adress == "com"){ | |
return "Yes"; | |
} | |
} | |
} | |
return "NO"; | |
} | |
int main(const int argc, const char **argv) | |
{ | |
if(argc < 2){ | |
std::cout << "usage: " << argv[0] << " [email protected]" << std::endl; | |
return EXIT_FAILURE; | |
} | |
std::cout << isValid(argv[1]) << std::endl; | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment