1

欢迎。我有两个问题。首先 - function size bool (const char * pass) 检查字符串中的字符数是否至少为 8,但有问题。它总是显示至少有 8 个字符,即使字符串只包含 3 个字符。

我的工作是创建几个小函数来检查输入的字符串的正确性。你能帮你解决这个问题吗?如果 bool check(...) 中的所有小函数都返回 true,我需要在控制台中写入“STRING IS OKAY”。

我将不胜感激任何建议。

    #include <iostream>
    #include <cctype>     
    using namespace std;

    //Check the amount of chars
    bool size (const char* pass){
        if(sizeof(pass) > 7)
                        return true;
    }

    //Checks if the ASCII are located between 32 to 126
    bool isPrint (const char* pass){
        for(int x=0; x <= sizeof(pass); x++){
            if(isprint(pass[x]))
                return true;
        }
    }


    //Check the amount of numbers
    bool isNum (const char* pass){
        for(int x=0; x <= sizeof(pass); x++){
            if(isdigit(pass[x]))
                return true;
        }
    }

    //Check the amount of Upper letters
    bool isUpperLetter (const char* pass){
        for(int x=0; x <= sizeof(pass); x++){
            if(isupper(pass[x]))
                return true;
        }       
    }

    //Check the amount of lower letters
    bool isLowerLetter (const char* pass){
        for(int x=0; x <= sizeof(pass); x++){
            if(islower(pass[x]))
                return true;
        }       
    }

    //Check the amount of Punctuation Marks
    bool isPunctMark (const char* pass){
        for(int x=0; x <= sizeof(pass); x++){
            if(ispunct(pass[x])){
                return true;
            }
        }       
    }


    //All small moduls together
    bool check (const char* pass){
        size(pass);
        isPrint(pass);
        isNum(pass);
        isUpperLetter(pass);
        isLowerLetter(pass);
        isPunctMark(pass);
    }

    int main() {
        char x;
        cout << "Enter the string of characters" << endl;
        cin >> x;
        const char *password = &x;
        check(password);
    }
4

4 回答 4

4

sizeof(pass)返回指针的大小。这是特定于实现的,如果你的函数总是返回 true,我们可以猜测,sizeof(char*)8意味着你有一个 64 位系统。

在许多其他系统上,它将返回4,甚至可能返回21,取决于架构。

您可能想检查指针指向的字符串的长度,如下所示:

int len=strlen(pass);
if(len>=8)   //Check for >=8 is clearer than >7)
{
   return true;
}

您还可以遍历字符串并检查是否为空。但是,当有一个很好的标准库例程可以完成这项工作时,为什么还要打扰呢。

运行所有检查,做类似的事情

bool isValid(const char* const pass)
{
     if(!isPrint(pass))
     {

         return false;
     }
     if (!isNum(pass))
     {
         return false;
      }
     //etc
 }

你也可以有一个大长

if(isPrint(pass)&&isNum(pass) .....)
{
    return true;
}

但这会更混乱,更难调试。

于 2013-11-01T08:45:43.710 回答
3

sizeof给你传递给它的对象类型的大小。它在编译时被严格评估。您正在传递它 a const char *,它在位系统上864

要获取C-style 字符串的长度,可以使用C函数strlen, 在 header 中<cstring>

也就是说,我建议要这样做。我建议从C字符串移到C++ std::strings,因为它们更容易正确使用。

现在,您非常不正确地使用C字符串!

int main() {
    char x;
    cout << "Enter the string of characters" << endl;
    cin >> x;
    const char *password = &x;
    check(password);
}

您读取单个 char( x),然后获取其地址并将其视为C字符串。现在这里有两个重要的问题。

首先,您可能打算阅读多个字符。

其次,您将遇到未定义的行为,并且您的计算机很可能会崩溃,因为C字符串应该是指向以.NUL结尾的数组的指针char。任何期望C字符串的函数都会循环查找没有结尾的结尾'\0'x因为它甚至不是一个数组。

因此,如果您std::string<string>标头中使用,您可以拥有更安全的代码,而无需使用指针、-NUL终止符等。

(未经测试)

// one of your functions for example
bool isUpperLetter (const std::string& s){
    for(int x=0; x < s.size(); ++x){ // Use <, not <=. C++ uses 0-indexing.
        if(isupper(s[x]))
            return true;
    }
    return false; // you forgot this!
}


int main() {
    std::string s;
    std::cout << "Enter a string:\n";
    std::cin >> s;

    isUpperLetter(s);
}

顺便说一句,如果您的输入字符串包含空格,这将不起作用,但一次只有一件事!

(如果你学得很快,接下来的步骤:阅读std::getline<algorithm>标题。std::count_if看起来非常相关。)

当我们这样做的时候,尽早改掉坏习惯,并阅读为什么你应该避免using namespace std;std::endl.

编辑

从您的评论中,您被签名卡住了bool check(const char*),所以我想您应该学习如何使用C字符串。让我们暂时假设您的教练知道他们在做什么。

然后循环遍历C字符串的正常方法是使用指针,检查'\0'. 因此,例如,要计算大写字母的数量(实际上,对于真正的代码,您不会这样写。或者至少,如果您尝试在我正在从事的项目上进行,我强烈建议您修复它) :

int countUppercase (const char* c)
{
    if(NULL==c) return 0;

    int count = 0;
    for ( ; '\0' != *c ; ++c ) // loop while not found the NUL
    {
        if (isupper(*c))
            ++count;
    }

    return count;
}

我仍然强烈建议您阅读 astd::string如果您可以摆脱它。如果没有,你下一个最好的选择可能是std::istream::getline.

于 2013-11-01T09:00:22.523 回答
2

您正在检查sizeof(pass)const char* 的大小。您应该遍历数组并str[i]=='\0'改为查找。

编辑: 按照建议,您也可以使用 strlen() 函数。

于 2013-11-01T08:45:20.520 回答
1

正如已经提到的,您在函数的循环中使用 sizeof 指针,而不是传递字符串的实际长度。此外,有时函数之前的注释与它们所做的或必须做的不相符。例如

//Check the amount of numbers
bool isNum (const char* pass){
    for(int x=0; x <= sizeof(pass); x++){
        if(isdigit(pass[x]))
            return true;
    }
}

在评论中写着“检查数字的数量”。我认为您应该返回给定字符串中的位数。

所以我会用以下方式重写函数

//Check the amount of numbers
size_t DigitCount ( const char* pass )
{
    size_t count = 0;

    for ( ; *pass; ++pass )
    {
        if ( isdigit( *pass ) ) ++count;
    }

    return count;
}
于 2013-11-01T09:06:44.293 回答