Thread: What to use for searching var for char

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    19

    What to use for searching var for char

    I am wanting to search a variable that will contain a single character for a possibility of 10 characters. I am betting the best way would be to use an array for the possibilities but I don't know what to use and how to implement it. Would it be something similar to preg in php?

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    I'm not entirely sure what you mean. You have a single character and want to see if it is one of 10 possible characters? This would work:
    Code:
    if(strchr("0123456789", your_char) != NULL) { /* it is 0 or 1 or .. 9 */ }
    If you are, by chance, checking whether it is one of the digits from '0' to '9', you can use isdigit():
    Code:
    if(isdigit( (unsigned char)your_char )) { /* is a digit */ }
    strchr() is prototyped in string.h, isdigit() in ctype.h. Unfortunately that cast to unsigned char is probably necessary, so it's not as clean as you might hope.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. silly printf("%d", var) question
    By nacho4d in forum C Programming
    Replies: 2
    Last Post: 10-30-2008, 04:08 PM
  2. Need help assignment
    By 6kaine9 in forum C Programming
    Replies: 26
    Last Post: 10-19-2008, 08:51 PM
  3. server question
    By xddxogm3 in forum Tech Board
    Replies: 24
    Last Post: 01-21-2004, 01:12 AM
  4. Void functions
    By cap in forum C Programming
    Replies: 16
    Last Post: 09-04-2002, 08:08 AM
  5. Vigenere Decipher/Encipher
    By Xander in forum C++ Programming
    Replies: 5
    Last Post: 02-15-2002, 09:24 AM