Thread: Alphanumeric check

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    18

    Question Alphanumeric check

    I'm trying to check to see if a string from standard input is alphanumeric or not. I feel as though there may be a function for this but...
    Code:
     for(test_pass = i; test_pass < i; i++){ 
           if(test_pass[i] == ('1', '2', '3', '4', '5', '6', '7', '8', '9', '0'))
                printf("Your password is alphanumeric!\n");
        }
    The compiler wasn't too happy with this. Any solutions?
    Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Look in ctype.h
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    11
    Well, you're trying to compare one fix value with an array.
    what you need is a function that tests if the specified char is alphanumeric
    2 immediate ways:
    you could test if the char is part of your collection with

    Code:
    char *strstr(const char *haystack, const char *needle);
    but your collection aka array is wrong initialized, if I'm not confused, you have to initialize it like
    Code:
    {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}
    you also could write your own method which checks every array element if it is the same as the specified one in a for loop


    Other approach: Since chars are ASCII and numbers are neighbours in ASCII Code, you can compare it like this


    Code:
    char c = '2'
    if ('0' < c) {
       printf("%c \n", c);
    }

    which ought to print out 2

    EDIT:

    When I take a second look at your code, i see that your for-loop probably won't work this way, what are you trying to accomplish? What is safed in test_pass?
    if test_pass is an array, as i suppose, then you have to init the for head in another way. You want to start with first letter, which will be a position 0, so you init i at zero
    Till when you want to check this string? To the end? than you have to continue loop until your 'i' which represents one char of your string, is the same position as the length of your string / array
    for example:

    Code:
    char str[] = "Hello World";
    for (int i = 0;i < strlen (str) ; i++) /* ++i or i++ doesn't really matter */
    {
        printf("%c", str[i]);
    }
    Last edited by fendor; 12-25-2014 at 05:18 PM. Reason: Added explanation

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The compiler isn't happy, because the loop is treating test_pass like an int, but the body of the loop is treating it like an array or a pointer. That is impossible - there is no type in C with that combination of properties.

    Assuming test_pass is an array (not an int) the if() statement may actually compile. However, it won't do anything like what you expect. The comma operator - used in an expression like "a,b" evaluates a, discards it, and then gives the result of b. The brackets simply stop interactions with other operators of different precedence, and yields the result of whatever is enclosed. So "if(test_pass[i] == ('1', '2', '3', '4', '5', '6', '7', '8', '9', '0'))" is equivalent to "if(test_pass[i] == '0')

    Look up isnum() and isalpha() functions in the standard header <ctype.h>. They provide a means of testing single characters are numeric or alphabetic, respectively. They don't work on arrays though - you will need to work out how to apply those functions to every element in an array.





    Seriously, though, you need to read a basic textbook on C before you provide more code. Your code here is obviously produced by pure guesswork. That is not a good way to learn (since you won't understand why the compiler complains or code doesn't work). It is also a way to seriously discourage people from helping you in forums, since it demonstrates low effort by you to learn, while expecting people to expend considerably more effort than you do, in order to help you.
    Last edited by grumpy; 12-25-2014 at 04:58 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Dec 2014
    Posts
    18
    Well thought i'd update. Taking Grumpy's advise and buying a C textbook, I found a simple solution.
    Code:
     for(i= 0; i < strlen(test_pass); i++){
            if(test_pass[i] == '\n')
            {
                test_pass[i] == '\0';
                break;
            }
            if   (test_pass[i] == '0' || test_pass[i] == '1' || test_pass[i] == '2' || test_pass[i] == '3'
               || test_pass[i] == '4' || test_pass[i] == '5' || test_pass[i] == '6' || test_pass[i] == '7'
               || test_pass[i] == '8' || test_pass[i] == '9')
            {
                j += 1;
            }
        }
    
            if(j==0)
            {
                printf("Your password contains NO numbers. Your password is not alphanumeric.\n");
            }
            else
            {
                printf("Your password contains numbers. Your password is alphanumeric.\n");
            }
    Thanks to all who helped.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I didn't advise you to do that.

    Buying a textbook was a good idea .... assuming you acquired a reasonable one. There are some pretty terrible ones around.

    Incidentally, "containing digits" and "alphanumeric" are not equivalent concepts.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I'm using this table : ASCII and Binary Characters (The Characters)

    And it seems like all character numbers have a value from 48 to 57.

    Couldn't you just iterate the string and do simple checks? Like,
    Code:
    if (char[i] >= 48 && char[i] <= 57)
    {
        /* do whatever */
    }

  8. #8
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    You can also use the function 'isdigit()':
    Code:
    #include <ctype.h>
    …
    if ( isdigit(char[i]) )
    {
        /* do whatever */
    }
    Other have classes, we are class

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by MutantJohn View Post
    I'm using this table : ASCII and Binary Characters (The Characters)

    And it seems like all character numbers have a value from 48 to 57.

    Couldn't you just iterate the string and do simple checks? Like,
    Code:
    if (char[i] >= 48 && char[i] <= 57)
    {
        /* do whatever */
    }
    Not all implementations employ ASCII, or related, character sets.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MutantJohn
    And it seems like all character numbers have a value from 48 to 57.

    Couldn't you just iterate the string and do simple checks?
    In the special case of the digits, you can indeed compare for the value being between '0' and '9', since that is guaranteed. However, comparing for the value being 48 and 57 is a bad idea, both because of the use of magic numbers and because those values depend on character set. Furthermore, other than for the digits, comparison for say, 'a' to 'z' makes assumptions that might not hold true.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by laserlight View Post
    In the special case of the digits, you can indeed compare for the value being between '0' and '9', since that is guaranteed. However, comparing for the value being 48 and 57 is a bad idea, both because of the use of magic numbers and because those values depend on character set. Furthermore, other than for the digits, comparison for say, 'a' to 'z' makes assumptions that might not hold true.
    Thank you. Using the '0' and '9' is significantly better than what I had in mind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SMS, PDU, and Alphanumeric Type of Number Problem
    By audinue in forum Tech Board
    Replies: 0
    Last Post: 12-02-2010, 11:27 PM
  2. Replies: 8
    Last Post: 02-14-2009, 11:33 PM
  3. Alphanumeric array
    By rehan in forum C++ Programming
    Replies: 14
    Last Post: 07-30-2007, 05:09 AM
  4. How to create a string of alphanumeric values???
    By symbee in forum C++ Programming
    Replies: 14
    Last Post: 09-28-2006, 02:13 AM
  5. Replace AlphaNumeric Characters
    By Hexxx in forum C++ Programming
    Replies: 3
    Last Post: 01-01-2006, 06:12 PM