简体   繁体   中英

Comparing each character of two strings in C

I am writing a program that allows the user to input a custom password, and then check if they meet specific criteria, in this case; 1. Must be between 9 and 15 characters. 2. Must have 2 or more of both uppercase and lowercase characters. 3. Must have 2 or more numbers. 4. must contain 1 or more symbols.

My problem is that I don't seem to be comparing the strings properly, as when I run it it says "Your string contains 0 lowercase, 0 uppercase, 0 symbols, 0 numbers", etc. I assume this is because it's not passing the requirements for the if statements. I have tried to use strcmp instead, but with no luck.

(Please bear with me as I am very new to C)

Here is my code:

#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");
}

Thanks in advance for any help offered.

This line

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

should look like this

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

As by initialising j to 0 and then testing j for being >= strlen(...) , with the latter most probably returning something greater 0 , the loop exits without any iteration.

You have some trouble with loop limits:

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

is immediately false, so everything inside this loop won't be executed.

Then, to "explore" a string from 0 to its last char, the check is

for (j = 0; j < length; j++)

and not <= .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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