Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

darxun

macrumors member
Original poster
Mar 18, 2008
40
0
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.
 

yeroen

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
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.
 

darxun

macrumors member
Original poster
Mar 18, 2008
40
0
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;
}
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
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.
 

darxun

macrumors member
Original poster
Mar 18, 2008
40
0
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;
}
 

iSee

macrumors 68040
Oct 25, 2004
3,540
272
That's what the length parameter is for.

Your call would be:

strncasecmp(Zeile, Aszkeys, strlen(Zeile))
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

darxun

macrumors member
Original poster
Mar 18, 2008
40
0
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>

Sorry, but I still don't really understand why I need "strlen(Aszkeys)==0" and not only some global number like "LENGTH".
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Sorry, but I still don't really understand why I need "strlen(Aszkeys)==0" and not only some global number like "LENGTH".


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:
The strcasecmp() and strncasecmp() return an integer greater than, equal
to, or less than 0, according as s1 is lexicographically greater than,
equal to, or less than s2 after translation of each corresponding charac-
ter to lower-case. The strings themselves are not modified. The compar-
ison is done using unsigned characters, so that `\200' is greater than
`\0'.

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
 

darxun

macrumors member
Original poster
Mar 18, 2008
40
0
Thank you. But what I actually want to know is, why I need "strlen(Aszkeys)" and not only any number.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Thank you. But what I actually want to know is, why I need "strlen(Aszkeys)" and not only any number.


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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.