c ++识别字符串中的特殊字符

Dav*_*ish 2 c++ string special-characters

我正在为学校编写一个程序来检查密码的强度并将它们分成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;
}
Run Code Online (Sandbox Code Playgroud)

joh*_*ohn 5

size_t special;

if (special != string::npos)
        specialChar = true;
Run Code Online (Sandbox Code Playgroud)

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

因为find_first_not_of返回索引而不是字符,所以必须声明specialsize_tnot char.