检查字符串是否包含非字母数字字符

时间:2013-01-13 22:40:43

标签: c++ string character non-alphanumeric

我正在尝试编写一个函数,该函数将字符串作为参数并检查该字符串是否只包含一个非字母数字字符,如果是这种情况则返回true,否则返回true假的。

例如:

'Alex's' would return true. 
James..Warner would return false.

我目前的代码如下,但我觉得它不起作用。因为我在其他地方有一个基本上算真实的数字。使用包含字符串的地图完成。我得到的计数值对于输入的单词来说太高了。

bool Class3::filter(string word)
    {
        string s = word;
        int len = s.size();

        for (int i=0; i<len; i++)
        {   if(!isalnum(s[i])){
            return true;}
            else{return false;}  
        }
     }

7 个答案:

答案 0 :(得分:5)

您可以使用std::count_if,然后检查该值是否大于1.

int i = std::count_if(str.begin(),str.end(),[](char c){ return !(std::isalnum(c)); });
return i > 1;

答案 1 :(得分:3)

你的程序在查看一个字符后做出决定;它不能像那样工作!当您看到该字符是字母数字时,您立即返回false,而不查看其余字符。要解决此问题,请将return false 移到循环外。

答案 2 :(得分:3)

其他人已经评论过您对问题的描述有多糟糕,并向您抛出了您可能不理解的基于模板的复杂代码。我强烈建议你阅读它;模板功能强大,实用,并且是一种很棒的编程技术。缺点是你必须先学习它们。

这是一个非面向模板的解决方案:

bool class3::filter(string word)
{
    //You aren't mutating word, so don't waste your time
    //(or memory) by copying it.  In fact, I'd (strongly)
    //recommend you utilize a constant pass by reference,
    //because by default that's what you're already doing,
    //so you were taking a copy of a copy.  Waste of memory!

    //Just init a count variable.
    int count=0;

    //Set up your for loop...
    for(int i=0; i<word.size(); i++)
    {
        if(!isalnum(word[i]))
        {
            //If a character doesn't match, increment your counter
            count++;
                            //If you want to, you can return false here if count>1 to improve efficiency -- depends on your end goal.
        }
    }
    //If you want exactly one non-alphanumeric, then return this.
    return count==1;
    //Or if it's a maximum of one non-alphanumeric, then...
    return count<=1;
    //Or you could generalize by returning a count of non alphanumerics -- remember to change the return type!
    return count;
}

答案 3 :(得分:2)

您可以将std::count_ifstd::isalnum结合使用。

bool filter(const std::string word)
{
  return std::count_if(word.begin(), word.end(), [](char c){ return !(std::isalnum(c));}) > 1;
}

需要注意的是,此算法会检查字符串中的所有字符。这可能是也可能不是性能问题。

答案 4 :(得分:2)

您可以使用std::count_iflambda

bool filter(const std::string& s)
{
  if (std::count_if(s.begin(), s.end(), [](char c){ return !std::isalpha(c); }) == 1)
  {
    cout << s << ": contains one non-alpha charectors" << endl;
    return false;
  }

 cout << s << ": string contains alpha charectors only" << endl;
 return true;      
}

答案 5 :(得分:2)

作为下一个人“效率低下”。

#include <string>
#include <algorithm>
#include <functional>
#include <cctype>

// return 'true' if there is one-and-only-one non-alphanumeric character
// in the given std::string parameter. If there are NO non-alphanumeric
// characters, OR more than one, then return 'false'.
bool filter(const std::string& s)
{
    function<bool(char)> is_not_alnum([](char c)->bool{ return !isalnum(c); });
    string::const_iterator first = find_if(s.begin(), s.end(), is_not_alnum);
    return first != s.end() && (find_if(first+1, s.end(), is_not_alnum) == s.end());
}

发布只是因为我也可以投票支持“那不是我怎么做”的原因。我宁愿和罪人一起笑,也不愿和圣徒一起哭泣。

答案 6 :(得分:-4)

您的代码中没有计算非字母数字字符的数量,是吗?你只需在第一个字符上返回true或false。

除非你算一算,否则你找不到答案。但是,你可以停在第二个非孤儿院。

由于您似乎需要编写代码的练习,这里有一些伪代码:

int nonalphas = 0;
for ( char in string )
    if ( char is nonalpha )
        nonalphas++;
        if ( nonalphas > 1 )
            break;
return nonalphas == 1;