a way to group characters?

This is a discussion on a way to group characters? within the C Programming forums, part of the General Programming Boards category; Okay I am writing a program where i need to check if a certain character is equal to 'C', 'A', ...

  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
    32,823
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 11:39 PM
  2. Replies: 10
    Last Post: 07-10-2008, 04:45 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  4. ray casting
    By lambs4 in forum Game Programming
    Replies: 62
    Last Post: 01-09-2003, 06:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21