strnicmp in xCode?

Discussion in 'Mac Programming' started by darxun, Mar 31, 2008.

  1. macrumors member

    Joined:
    Mar 18, 2008
    #1
    Hi,

    is there any function which does exactly the same like strnicmp in xcode? Or is there any way to use strnicmp in xcode?

    Greetz.
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    I think strncasecmp is what you want. See
    Code:
    man strncasecmp
     
  3. macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #3
    I'm not at my apple at the moment so I can't check to make sure, but the function:

    Code:
    int strncasecmp (const char *s1, const char *s2, size_t n)
    is in the GNU-C library as an extension and it performs case-insensitve C-string comparisons. Do a man on strncasecmp and see if it's there.
     
  4. thread starter macrumors member

    Joined:
    Mar 18, 2008
    #4
    I am using this code, and "strncasecmp" needs one more argument. If I just type in LENGTH it still tells me that there are too few arguments. LENGTH is defined(#define LENGTH 80).
    A number (i.e. 8) doesn't work as well.

    Code:
    int checkSchluessel(char* Aszkeys[], char* Zeile, int dim)
    {
        int i;
        for (i=0; i<dim; i++)
            if (strncasecmp(Zeile, Aszkeys[i])==0, [COLOR="Red"]LENGTH[/COLOR])
                return i;
        
        return _NOP;
    }
     
  5. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #5
    Code:
    for (i=0; i<dim; i++){
          if (strncasecmp(Zeile, Aszkeys[i],[b]LENGTH[/b])==0){
               return i;
          }
    }
    
    You put LENGTH in the wrong place ;), it should be inside the bracket as shown in bold.
     
  6. thread starter macrumors member

    Joined:
    Mar 18, 2008
    #6
    Ok, no that was nice^^.
    But the function does not work exactly like strnicmp. I'm trying to detect keywords like "add, mul, etc..." As long as they are at the start of a string, they should be detected. But this function only detects things like "MUL, mul, ADD, add" but not "addweowg, ADDEPWNG". The last two strings contain keywords at the beginning of the string.

    Here is the complete code:

    Code:
    #include "stdio.h"
    #include "stdlib.h"
    #include "string.h"
    #include "math.h"
    
    #define LENGTH 80
    #define _NOP 99
    
    int checkSchluessel(char* Aszkeys[], char* Zeile, int dim)
    {
        int i;
        for (i=0; i<dim; i++)
            if (strncasecmp(Zeile, Aszkeys[i], LENGTH)==0)
                return i;
        
        return _NOP;
    }  
    
    
    int main(void)
    {
        typedef enum {EXIT, SET, ADD, SUB, MUL, DIV, NOP=_NOP} KEY;
    
        char szTempstr[LENGTH];
        char *AszSchluesselWoerter[] = {"exit", "set", "add", "sub", "mul", "div"};
        int dim = 6, i=0;
        KEY input = EXIT;
    
        do
        {
            for (i = 0; i < LENGTH; i++)
                szTempstr[i] = 0;
    
            printf(">");
            gets(szTempstr);
    
            input = (KEY)checkSchluessel(AszSchluesselWoerter, szTempstr, dim);
    		
            switch(input)
            {
                case EXIT:
                {
                    printf("Exit Program");
                    break;
                }
                case SET:
                {
                    printf("SET erkannt");
                    break;
                }
                case ADD:
                {
                    printf("ADD erkannt");
                    break;
                }
                case SUB:
                {
                    printf("SUB erkannt");
                    break;
                }
                case MUL:
                {
                    printf("MUL erkannt");
                    break;
                }
                case DIV:
                {
                    printf("DIV erkannt");
                    break;
                }
                case NOP:
                {
                    printf("Nichts erkannt!");
                    break;
                }
            }
            printf("\n\n");
        }
        while(input != EXIT);
    return 0;
    }
     
  7. macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #7
    That's what the length parameter is for.

    Your call would be:

    strncasecmp(Zeile, Aszkeys, strlen(Zeile))
     
  8. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    Code:
    <snip>
    int checkSchluessel(char* Aszkeys[], char* Zeile, int dim)
    {
        int i;
        for (i=0; i<dim; i++)
            if (strncasecmp(Zeile, Aszkeys[i], strlen(Aszkeys[i])==0)
                return i;
        
        return _NOP;
    }  
    
    <snip>
    
    It works fine, but the length you were giving was too long. You want to do a prefix match, as I understand it. The code above should do what you want. It will compare the number of characters in the operation, so as long as that's how the other string starts, you should get a match.

    -Lee
     
  9. thread starter macrumors member

    Joined:
    Mar 18, 2008
    #9
    Thanks, it does work well!
     
  10. thread starter macrumors member

    Joined:
    Mar 18, 2008
    #10
    Sorry, but I still don't really understand why I need "strlen(Aszkeys)==0" and not only some global number like "LENGTH".
     
  11. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #11


    You are comparing:
    strncasecmp(Zeile, Aszkeys, strlen(Aszkeys))
    to
    0

    In the example above (that I sent you, oops), the closing parentheses for the strncasecmp invocation is missing. That might be leading to some of your confusion.

    From the strncasecmp man page:
    When the two strings are lexicographically equal to one another, strncasecmp will return 0. You could just use !strncasecmp(..), but in my opinion that is far less clear than an explicit check of equivalence to 0.

    -Lee
     
  12. thread starter macrumors member

    Joined:
    Mar 18, 2008
    #12
    Thank you. But what I actually want to know is, why I need "strlen(Aszkeys)" and not only any number.
     
  13. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #13


    Aszkeys[0] is 'exit'. You want this to match things like 'EXITro'(just an example). You need to only compare the first 4 characters (the len of Aszkeys[0]), because the fifth character of Aszkeys[0] is the null terminator and the fifth character of Zeile is r. That means if you compare more than 4 characters you will not have a match.

    Does that make sense?

    -Lee
     

Share This Page