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

时间:2014-01-04 17:38:27

标签: c++ 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");
}

提前感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:2)

这一行

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

应该是这样的

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

首先将j初始化为0,然后将j作为>= strlen(...)进行测试,后者最有可能返回更大的0,循环退出没有任何迭代。

答案 1 :(得分:2)

循环限制有问题:

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

立即为false,因此此循环内的所有内容都不会被执行。

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

for(j = 0; j&lt; length; j ++)

而不是<=