best way to base condition on string

This is a discussion on best way to base condition on string within the C Programming forums, part of the General Programming Boards category; What is the best way to do a "switch" equivelant for a string. I have been using if ( strncmp( ...

  1. #1
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222

    best way to base condition on string

    What is the best way to do a "switch" equivelant for a string.

    I have been using

    if ( strncmp( string1, string2, s ) == 0 )
    else if ( strncmp( string1, string3, s ) == 0 )
    else if ( strncmp( string1, string4, s ) == 0 )
    etc


    Is there a more efficient way of doing this?
    Last edited by Kinasz; 07-26-2004 at 08:39 AM.
    "Assumptions are the mother of all **** ups!"

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    nope - that's how it's done. of course, placing the strings in an array might clean up the code quite a bit and reduce the size of the program somewhat.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    ok, thanks I just wasnt sure if I was doing something stupid or not.
    "Assumptions are the mother of all **** ups!"

  4. #4
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Kinasz
    What is the best way to do a "switch" equivelant for a string.

    I have been using

    if ( strncmp( string1, string2, s ) == 0 )
    else if ( strncmp( string1, string3, s ) == 0 )
    else if ( strncmp( string1, string4, s ) == 0 )

    Is there a more efficient way of doing this?
    For less than, say 20 strings, no. For 20 to 100, a linear search is fine. For more strings, a bsearch() in a sorted array is probably more efficient. For huge number of complete strings (strcmp()) some hashed-based code is probably the fastet way to do it.
    Emmanuel Delahaye

    "C is a sharp tool"

  5. #5
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    sweet I will look into it
    "Assumptions are the mother of all **** ups!"

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    lowercaseyourstring( whatever );
    switch( *whatever )
    {
        case 'a':
            if( !strcmp( whatever, "aardvark" )
            {
            }
            else
            if( !strcmp( whatever, "apple" )
            {
            }
            else
            if( !strcmp( whatever, "anything" )
            {
            }
            ...
        break;
        case 'b':
            if( !strcmp( whatever, "boy" )
            {
            }
            else
            ...
        break;
        ...
    }
    I suppose it all depends on exactly what you're doing...

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by quzah
    Code:
    lowercaseyourstring( whatever );
    switch( *whatever )
    {
        case 'a':
            if( !strcmp( whatever, "aardvark" )
            {
            }
            else
        ...
    }
    That's a smart compromize. I'll try to recall it. Thanks.
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM

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