Thread: Beginner question - dealing with blank non-null characters

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    1

    Beginner question - dealing with blank non-null characters

    Hey guys, just starting to learn C, and have a question.
    I'm going through this tutorial series: https://www.youtube.com/watch?v=PVTD...6nhGJ9Vlcjyymq

    And the first 'challenge' given by the presenter is giving me problems. Here's the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #include <math.h>
    
    int main(){
        char userPassword[30];
        printf("Enter a password: ");
        scanf(" %s", userPassword);
        char blank[1];
    
        int trueA, trueB, trueC;
        trueA = trueB = trueC = 0;
        int passLoop = 0;
        while (passLoop != 31) {
            if ((isalpha(userPassword[passLoop]) == 1)&&(isupper(userPassword[passLoop]) == 1)){printf("ALPHA!"); trueA = 1;}
            if (isdigit(userPassword[passLoop]) == 1) {printf("DIGIT!"); trueB = 1;}
            if ((isascii(userPassword[passLoop]) == 1)&&(isgraph(userPassword[passLoop]))&&(ispunct(userPassword[passLoop]))) {printf("ASCII!"); trueC = 1;}
            passLoop++;
        }
        if ((trueA == 1)&&(trueB == 1)&&(trueC == 1)) {printf("Password accepted!");}
        else {printf("Password cannot be used!");}
    
        return 0;
        }
    The user is supposed to input a password that contains at least one upper case letter, one number, and one symbol. If it contains all three of those things it returns "Password accepted!", if not it returns "Password cannot be used!". The problem is with the symbol part, as I have absolutely no idea how to determine which characters in the array have been left unused. As a result it always returns false positives.

    Can any of you experienced C coders explain to me what the correct way to deal with this is?
    Last edited by MrHobbyBot; 01-25-2016 at 03:59 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    isspace() is the correct function to use. It returns true for any white space character. Be aware that using %s in scanf() means that the password won't contain white space though, so it is a pointless test. scanf() will actually stop reading a string for %s after it finds white space.

    Edit: Oh, it seems like your question has changed - or maybe I read it wrong in the first place. Well, it depends on what you want to call a symbol. To me, at least, it is a non-alpha-numeric character.
    Code:
    if (!isalnum(userPassword[passLoop]))
    The question is, is this good enough? I played the video, it seemed like he wanted something to match '$' only. Perhaps you are expanding it, but a direct comparison would suffice.
    Last edited by whiteflags; 01-25-2016 at 04:17 PM.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by MrHobbyBot View Post

    Code:
            if ((isalpha(userPassword[passLoop]) == 1) ...
    Don't perform tests like this using == 1. Using the 'is' functions should be done like this:

    Code:
        if (isalpha(ch)) 
            /* do something */
    The reason is that isalpha and most other functions returning a yes/no answer are allowed to return any nonzero value to represent "yes". Testing against 1 in particular may work sometimes but it is not correct. Your test might fail when you least expect it because one of those functions decided to return, say, 53 or -128 instead of 1.

  4. #4
    Registered User
    Join Date
    Dec 2015
    Posts
    68
    If, else if... is perfect for this
    Code:
    testresult =0; i=strlen(password);
    while(i--){
    if (password[i] >= 'a' && password[i] =< 'z') testresult |= 1;
    else if (password[i] >= 'A' && password[i] =< 'Z') testresult |= 2;
    else if (password[i] >= '0' && password[i] =< '9') testresult |= 4;
    else if (password[i] >= '!' && password[i] =< '/') testresult |= 8;
    }
    if (testresult == 15)  OK code;
    NOT tested and some special char are not included
    Last edited by tonyp12; 01-26-2016 at 02:06 PM.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I prefer:
    Code:
    int test_password(const char *password) {
      int hasLower=0, hasUpper=0, hasDigit=0, hasPunct=0;
      for ( ; *password; password++) {
        if      (islower(*password)) hasLower = 1;
        else if (isupper(*password)) hasUpper = 1;
        else if (isdigit(*password)) hasDigit = 1;
        else if (ispunct(*password)) hasPunct = 1;
      }
      return hasLower && hasUpper && hasDigit && hasPunct;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dealing Poker Hands for a beginner
    By jp50pr in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2012, 03:16 PM
  2. Replies: 5
    Last Post: 05-25-2012, 09:27 PM
  3. Comparing string for blank characters - C programming
    By dannydb368 in forum C Programming
    Replies: 3
    Last Post: 05-22-2010, 10:14 PM
  4. Replies: 4
    Last Post: 10-29-2008, 03:22 AM
  5. Replies: 9
    Last Post: 11-27-2001, 09:25 AM