c++ - 尝试使用 C++ 从列表中打印无效的电子邮件地址?
问题描述
我正在尝试从电子邮件地址列表中打印无效电子邮件地址列表(其中包含空格且没有 @ 或 .)。该列表有一些带有空格的电子邮件地址,并且没有“@”或“。” 但它仍然没有打印任何东西。
//Declaring boolean variables
bool atPresent;
bool periodPresent;
bool spacePresent;
string emailid = someemailfrom a list;
atPresent = false;
periodPresent = false;
spacePresent = false;
//looking for @
size_t foundAt = emailid.find('@');
if (foundAt != string::npos) {
atPresent = true;
}
//looking for '.'
size_t foundPeriod = emailid.find('.');
if (foundPeriod != string::npos) {
periodPresent = true;
}
//looking for ' '
size_t foundSpace = emailid.find(' ');
if (foundSpace != string::npos) {
spacePresent = true;
}
//checking to see if all conditions match
if ( (atPresent == false) && (periodPresent == false) && (spacePresent == true)) {
cout << emailid << endl;
}
解决方案
(atPresent == false) && (periodPresent == false) && (spacePresent == true)
是错的。仅当满足无效地址的所有三个条件时,它才是正确的。但是,只要满足至少一个标准,地址就无效。这将是
(atPresent == false) || (periodPresent == false) || (spacePresent == true)
并简化:
!atPresent || !periodPresent || spacePresent
推荐阅读
- javascript - PayPal预先批准的付款按钮
- azure-devops - 如何在 azure devops 管道中运行 azure cli 命令?
- batch-file - 从变量中删除空格不起作用?
- python - 比较字典中列表之间的整数
- r - 测试跨因子计算的边际效应之间的差异
- javascript - 使用 "getElementsByTagName("li").length" 时仅获取子 LI
- javascript - 如何在不刷新页面的情况下在同一页面上动态获取 php 数据
- html - 避免预包装 div 内的顶部填充
- python - 使用正则表达式过滤目录并将过滤后的文件输出到另一个目录
- javascript - 使用 getElementsByClassName 计算输入字段值