【问题标题】:C++ Check if String contains atleast 1 digit and 1 letterC++ 检查字符串是否包含至少 1 个数字和 1 个字母
【发布时间】:2018-01-04 15:11:42
【问题描述】:

我正在尝试在返回 true 之前检查字符串输入是否至少包含一个数字和一个字母。

我的方法是首先循环遍历单词。 如果单词不包含isalphaisdigit,则返回false。

在那次检查之后,我创建了两个计数(一个用于数字,一个用于字母)。我检查isalpha 是否在计数中加一。然后我检查是否isdigit 并添加到该计数中。

最后,我检查两个计数是否大于或等于 1(意味着它至少包含一个数字和一个字母)并返回 true。我知道计数不是最好的方法,但我只是想测试该方法,但它无法正常工作,我不确定我的逻辑哪里错了。

bool isDigitLetter::filter(string word) {
    int digit = 0;
    int letter = 0;
    if(word.empty()) {
        return false;
    }
    for(int i = 0; i < word.length(); i++) {
        if(!isalpha((unsigned char)word[i]) || !isdigit((unsigned char)word[i])) {
            return false;
        }
    }
    for(int x = 0; x < word.length(); x++) {
        if(isalpha((unsigned char)word[x])) {
                letter+=1;
        }
        if(isdigit((unsigned char)word[x])) {
            digit+=1;
        }
    }
    if(digit && letter>= 1) {
        return true;
    }
}

我在想也许可以使用isalnum,但如果它包含任何一个,但不检查它是否至少包含一个,这将返回 true。

【问题讨论】:

  • 使用调试器并逐步执行程序。查看与您期望的不同之处并更详细地检查该行。
  • 使用调试器。
  • if(digit &amp;&amp; letter&gt;= 1) 这永远是真的
  • bool isDigitLetter::filter(std::string word){ return std::any_of(word.begin(), word.end(), [](char c){ return std::isalpha(c); }) &amp;&amp; std::any_of(word.begin(), word.end(), [](char c){ return std::isdigit(c); }); }
  • “至少 1”不需要完整计数。一个循环就足够了;它只需要跟踪是否它看到了一个数字和一个字母,一旦看到了两者,它就完成了。

标签: c++


【解决方案1】:
bool isDigitLetter::filter(string word) {
    bool hasLetter = false;
    bool hasDigit = false;
    for (int i = 0; i < word.size(); i++) {
        if (isdigit(word.at(i))) { hasDigit = true; }
        if (isalpha(word.at(i))) { hasLetter = true; }
    }
    return (hasLetter && hasDigit);
} 

这个解决方案去掉了很多不必要的代码。

基本上,它遍历字符串并检查每个字符是字母还是数字。每次看到一个,它都会更新 hasLetter/hasDigit 变量。如果两者都为真,则返回真,否则返回假。

编辑:这个解决方案更快 - 如果它已经看到一个字母和一个数字,它会立即返回。

bool isDigitLetter::filter(string word) {
    bool hasLetter = false;
    bool hasDigit = false;
    for (int i = 0; i < word.size(); i++) {
        if (isdigit(word.at(i))) { hasDigit = true; }
        if (isalpha(word.at(i))) { hasLetter = true; }
        if (hasDigit && hasLetter) { return true; }
    }
    // we got here and couldn't find a letter and a digit
    return false;
} 

【讨论】:

  • 谢谢,我相信您的 if 语句,您缺少一个括号?您打开 3 个,但总共关闭 2 个。
  • 嗯,让我看看
  • 以及返回时的分号(编辑前)
  • 哦,好的,找到了-正在修复...已编辑-这样好吗? @godlycplusplus
  • 就是这样。以及return (hasLetter &amp;&amp; hasDigit) 之后的分号
【解决方案2】:

应该是这样的:

bool isDigitLetter::filter(string word) {

    int digit = 0;
    int letter = 0;
    if(word.empty()) {
        return false;
    }
//    for(int i = 0; i < word.length(); i++) {
//        if(!isalpha((unsigned char)word[i]) || !isdigit((unsigned char)word[i])) {
//            return false;
//        }
//    }
    for(int x = 0; x < word.length(); x++) {
        if(isalpha((unsigned char)word[x])) {
                letter+=1;
        }
        if(isdigit((unsigned char)word[x])) {
            digit+=1;
        }
    }
    return ((digit > 0) && (letter > 0));
}

【讨论】:

  • 如果letterdigit 都大于0,循环也可以提前终止。
  • 还要注意,return 语句中的括号是多余的。 return digit &gt; 0 &amp;&amp; letter &gt;0; 做同样的事情。
  • 只是开玩笑,没有冒犯:)
  • @p-a-o-l-o -- 多余的括号使代码更难阅读;你必须弄清楚它们是否真的意味着什么。初学者喜欢它们,但程序员不只是初学者,需要学习编写代码而不增加干扰。
  • 是的,任何系统都可以从 0.1 微秒的提升中获得惊人的收益。 :)
【解决方案3】:

你的问题在于这一行:

if(digit && letter>= 1)

改成:

if(digit >=1 && letter>= 1)

【讨论】:

  • 嗯……不完全是。
  • if (digit) 等价于if (digit != 0);是的,这与检查 &gt;= 1 不同,因为它也适用于 digit 的负值,但由于 digit 从 0 开始并且只会上升,它们会做同样的事情。
  • 您的编辑并没有使它成为更好的答案。那条线不是问题。
  • @SandraK 我知道这是一个逻辑错误,但意外地它并没有影响代码执行。无论如何,必须纠正。
猜你喜欢
  • 2010-12-28
  • 1970-01-01
  • 2011-10-13
  • 2016-07-20
  • 1970-01-01
  • 1970-01-01
  • 2011-12-12
  • 2017-05-13
  • 1970-01-01
相关资源
最近更新 更多