fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int countAt(string &s)
  5. {
  6. int amount = 0;
  7.  
  8. for (char si : s)
  9. {
  10. if (si == '@')
  11. amount++;
  12. if (amount > 1)
  13. return false;
  14. }
  15.  
  16. return amount == 1;
  17. }
  18.  
  19. bool checkLocal(string &local)
  20. {
  21. for (char si : local)
  22. {
  23. if ('A' <= si && si <= 'Z')
  24. continue;
  25. if ('a' <= si && si <= 'z')
  26. continue;
  27. if ('0' <= si && si <= '9')
  28. continue;
  29. if (si == '.' || si == '_')
  30. continue;
  31. return false;
  32. }
  33. return true;
  34. }
  35.  
  36. bool checkDomain(string &domain)
  37. {
  38. for (char si : domain)
  39. {
  40. if ('A' <= si && si <= 'Z')
  41. continue;
  42. if ('a' <= si && si <= 'z')
  43. continue;
  44. if (si == '.')
  45. continue;
  46. return false;
  47. }
  48.  
  49. int amount = 0;
  50.  
  51. for (char si : domain)
  52. {
  53. if (si == '.')
  54. amount++;
  55. }
  56.  
  57. if (amount == 0)
  58. return false;
  59.  
  60. for (int i = 0; i < domain.length() - 1; i++)
  61. {
  62. if (domain[i] == '.' && domain[i + 1] == '.')
  63. return false;
  64. }
  65. return true;
  66. }
  67.  
  68. bool check(string &s)
  69. {
  70. if (countAt(s) == false)
  71. return false;
  72.  
  73. string local = "";
  74. string domain = "";
  75.  
  76. int i = 0;
  77. while (s[i] != '@')
  78. {
  79. local += s[i];
  80. i++;
  81. }
  82.  
  83. i++;
  84.  
  85. while (i < s.length())
  86. {
  87. domain += s[i];
  88. i++;
  89. }
  90.  
  91. if (local.length() == 0 || domain.length() == 0)
  92. return false;
  93.  
  94. if (checkLocal(local) == false)
  95. return false;
  96.  
  97. if (checkDomain(domain) == false)
  98. return false;
  99. return true;
  100. }
  101.  
  102. int main()
  103. {
  104. string s;
  105. getline(cin, s);
  106.  
  107. cout << (check(s) ? "VALID\n" : "INVALID\n");
  108.  
  109. return 0;
  110. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
INVALID