C++ 标识字符串中的特殊字符

c++ identifying special characters in a string

本文关键字:特殊字符 字符串 标识 C++      更新时间:2023-10-16

我正在为学校编写一个程序,该程序应该检查密码的强度并将它们分成 3 个参数。我在识别强字符中的特殊字符以对强字符进行分类时遇到问题。任何帮助将不胜感激。

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string input;
    bool complete = false;
    bool hasUpper = false;
    bool hasLower = false;
    bool hasDigit = false;
    bool specialChar = false;
    int count;
    char special = 'a';

    do
    {
        cout << endl << "Enter a password to rate its strength. Enter q to quit." << endl;
        cin >> input;
        for(count =0; count < input.size(); count++)
        {
            if( islower(input[count]) )
            hasLower = true;
            if( isupper(input[count]) )
            hasUpper = true;
            if( isdigit(input[count]) )
            hasDigit = true;
            special = input.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ");
            if (special != 'a')
            specialChar = true;
        }
        if (hasLower && hasUpper && hasDigit && specialChar && (count >= 8))
        {
            cout << "Strong" << endl;
        }
        else if((hasLower || hasUpper) && hasDigit && (count >= 6))
        {
            cout << "Moderate" << endl;
        }
        else
        { 
            cout << "Weak" << endl;
        }
        if (input == "q") complete = true; 
    }while (!complete);
    return 0;
}
size_t special;
if (special != string::npos)
        specialChar = true;

find_first_not_of返回找到的字符的索引,如果未找到字符,则返回特殊值string::npos

由于find_first_not_of返回的是索引而不是字符,因此必须将special声明为 size_t而不是char

这更像是对代码结构的注释,而不是直接的回答您的直接问题。 (如果更正但是,问题将消失。 现在您似乎混合了两种不同的解决方案,非常奇怪道路。 特别是,你打电话给input.find_first_not_of每次通过循环,尽管它检查了所有的字符。 您应该选择一个解决方案,并使用它对于所有条件。

如果要循环,请检查每个字符:

for ( int count = 0; count != input.size(); ++ count ) {
    unsigned char ch = input[count];    // To avoid undefined behavior
    if ( islower( ch ) {
        hasLower = true;
    } else if ( isupper( ch ) ) {
        hasUpper = true;
    } else if ( isdigit( ch ) ) {
        hasDigit = true;
    } else {
        hasSpecial = true;
    }
}

请注意,使用 if/else if 意味着您不需要特殊测试 - 特殊是任何不符合任何要求的东西前面的测试。 如果您想要测试,!isalnum( ch )就可以达到目的。

或者,您可以对每个函数使用标准函数:

hasLower = std::find_if(
                input.begin(),
                input.end(),
                []( unsigned char ch ) { return islower( ch ); } )
            != input.end();
hasUpper = std::find_if(
                input.begin(),
                input.end(),
                []( unsigned char ch ) { return isupper( ch ); } )
            != input.end();
hasDigit = std::find_if(
                input.begin(),
                input.end(),
                []( unsigned char ch ) { return isdigit( ch ); } )
            != input.end();
hasSpecial = std::find_if(
                input.begin(),
                input.end(),
                []( unsigned char ch ) { return !isalnum( ch ); } )
            != input.end();

上述 lambda 函数仅在 C++11 中可用。如果您没有 C++11,则必须单独编写每个功能对象,这将使该解决方案走得更远比上面的简单循环重。 当然,除非你是做大量的文本处理,在这种情况下,功能性对象将放在您的工具包中,可以多次重复使用。除非您已准备好功能对象并在工具中Kit,但是,这似乎比简单的循环更复杂,甚至用λ。 (另一方面,它更惯用。 但那么,很难想象任何有经验的C++程序员他的工具包中没有功能对象。