Thread: a way to group characters?

  1. #1
    Unregistered
    Guest

    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
    Unregistered
    Guest
    You could use strstr, if you make your comparison a "string" instead of a character. You also could just do:
    Code:
    int hasc( char c, char *s )
    {
       int x;
       for(x=0;s&&s[x];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");

    Quzah.

  3. #3
    Unregistered
    Guest
    Ack. That should be:

    if( s[x] == c )

    Not:

    if( s[x] = c )

    (didn't feel like loggin in)

    Quzah.

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

    Code:
    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
    Unregistered
    Guest
    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.)

    Quzah.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    2
    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