繁体   English   中英

比较C中两个字符串的每个字符

[英]Comparing each character of two strings in C

我正在编写一个程序,该程序允许用户输入自定义密码,然后检查是否符合特定条件; 1.必须介于9到15个字符之间。 2.必须同时包含2个或多个大写和小写字符。 3.必须有2个或更多的数字。 4.必须包含1个或多个符号。

我的问题是我似乎没有正确比较字符串,因为在运行它时,它说“您的字符串包含0个小写字母,0个大写字母,0个符号,0个数字”,等等。我认为这是因为未传递if语句的要求。 我尝试使用strcmp代替,但是没有运气。

(因为我是C的新手,请多多包涵)

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>


char upCase [] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
char lowCase [] = {"abcdefghijklmnopqrstuvwxyz"};
char numbers [] = {"1234567890"};
char symbols [] = {"!#$%&'()*+-./:;<=>?@[\]^_`~"};
char passwordCheck [15+1];
int numCount = 0;
int lCaseCount = 0;
int uCaseCount = 0;
int symbolCount = 0;
int i = 0;
int randLCase, randNum, randUCase, randSymbol;
int charCount = 0;

void main()
{
    fflush(stdin);
    printf("\nEnter your password for checking: ");
    scanf("%s", passwordCheck);  // reads number
    if (strlen(passwordCheck) > 15)
    {
        printf("'%s' is too long.\nIt must be between 9 and 15 characters", passwordCheck);
    }
    else if (strlen(passwordCheck) < 9)
    {
        printf("'%s' is too short.\nIt must be between 9 and 15 characters", passwordCheck);
    }

    else
    {
        int j;
        int upCaseCheck, lowCaseCheck, symbolCheck, numCheck;
        printf("Checking password '%s'..\n", passwordCheck);

        for (j = 0 ; j >= strlen(passwordCheck); j++)
        {
            // check amount of uppercase characters in user's password
            for (upCaseCheck=0; upCaseCheck <= strlen(upCase); upCaseCheck++)
            {
                if (passwordCheck[j] == upCase[upCaseCheck])
                {
                    uCaseCount++;
                } 
            }
            // check amount of lowercase characters in user's password
            for (lowCaseCheck=0; lowCaseCheck <= strlen(lowCase); lowCaseCheck++)
            {
                if (passwordCheck[j] == lowCase[lowCaseCheck])
                {
                    lCaseCount++;
                }
            }
            // check amount of numbers in user's password
            for (numCheck=0; numCheck <= 15; numCheck++)
            {
                if (passwordCheck[j] == numbers[numCheck])
                {
                    numCount++;
                }
            }
            // check amount of symbols in user's password
            for (symbolCheck=0; symbolCheck <= strlen(symbols); symbolCheck++)
            {
                if (passwordCheck[j] == symbols[symbolCheck])
                {
                    symbolCount++;
                }
            }
        } // end outer for

        printf("Your password %s contains:\n %d numbers\n %d uppercase letters\n %d lowercase numbers\n %d symbols",
        passwordCheck, numCount, uCaseCount, lCaseCount, symbolCount);
        fclose(fp);
        printf("\n\n");
        system("pause");
}

在此先感谢您提供的任何帮助。

这条线

for (j = 0 ; j >= strlen(passwordCheck); j++) /

应该看起来像这样

for (j = 0 ; j < strlen(passwordCheck); j++)

就像通过将j初始化为0然后测试j是否为>= strlen(...) ,后者最有可能返回大于0 ,该循环无需任何迭代即可退出。

您在循环限制方面遇到了一些麻烦:

for (j = 0 ; j >= strlen(passwordCheck); j++)

立即为false,因此不会执行此循环中的所有操作。

然后,要“探索”从0到最后一个字符的字符串,检查是

对于(j = 0; j <长度; j ++)

而不是<=

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM