Thread: a way to group characters?

  1. #1

    a way to group characters?

    Okay I am writing a program where i need to check if a certain character is equal to 'C', 'A', 'R' and about 7 other letters. Now I was wondering if there was a way to make a short list in an if statement to check if they are equal.

    such as
    if (TheLine[i] != {'C', 'A', 'R', 'P', 'q', 's'}){


    but that doesn't work the way it does in other languages. Is there a way to do this, or do I need to do it in a different way? Thanks. If this post isn't clear in my question I will try asking it in another way. Thanks

  2. #2
    You could use strstr, if you make your comparison a "string" instead of a character. You also could just do:
    int hasc( char c, char *s )
       int x;
          if( s[x] = c ) return 1;
       return 0;
    There's a simple little function to do it for you. Call it like so:

    if ( hasc( letterToFind, "casdefq" ) ) printf("Ok, found it.\n");


  3. #3
    Ack. That should be:

    if( s[x] == c )


    if( s[x] = c )

    (didn't feel like loggin in)


  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Much easier is strchr

    if ( strchr( "CARP", ch ) != NULL ) {
      // found!!!
    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.

  5. #5
    That's what I was thinking of. (Originally I was thinking strstr, and I knew there was a function to do it, but I couldn't recall it off the top of my head--I didn't want to say 'strchr' and have it not be a standard function.)


  6. #6
    Registered User
    Join Date
    Aug 2001
    I would use the strcmp function and do a switch on the returned value against a pre-made list to see what char it is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. ray casting
    By lambs4 in forum Game Programming
    Replies: 62
    Last Post: 01-09-2003, 06:57 PM