PDA

View Full Version : strnicmp in xCode?




darxun
Mar 31, 2008, 12:33 PM
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.



kainjow
Mar 31, 2008, 12:43 PM
I think strncasecmp is what you want. See man strncasecmp

yeroen
Mar 31, 2008, 12:49 PM
I'm not at my apple at the moment so I can't check to make sure, but the function:

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
Mar 31, 2008, 01:10 PM
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.

int checkSchluessel(char* Aszkeys[], char* Zeile, int dim)
{
int i;
for (i=0; i<dim; i++)
if (strncasecmp(Zeile, Aszkeys[i])==0, LENGTH)
return i;

return _NOP;
}

Eraserhead
Mar 31, 2008, 01:13 PM
for (i=0; i<dim; i++){
if (strncasecmp(Zeile, Aszkeys[i],LENGTH)==0){
return i;
}
}


You put LENGTH in the wrong place ;), it should be inside the bracket as shown in bold.

darxun
Mar 31, 2008, 01:27 PM
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:

#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
Mar 31, 2008, 02:18 PM
That's what the length parameter is for.

Your call would be:

strncasecmp(Zeile, Aszkeys[i], strlen(Zeile))

lee1210
Mar 31, 2008, 02:22 PM
<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
Mar 31, 2008, 02:31 PM
Thanks, it does work well!

darxun
Apr 8, 2008, 10:13 AM
<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[i])==0" and not only some global number like "LENGTH".

lee1210
Apr 8, 2008, 11:17 AM
Sorry, but I still don't really understand why I need "strlen(Aszkeys[i])==0" and not only some global number like "LENGTH".

You are comparing:
strncasecmp(Zeile, Aszkeys[i], strlen(Aszkeys[i]))
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
Apr 9, 2008, 01:52 AM
Thank you. But what I actually want to know is, why I need "strlen(Aszkeys[i])" and not only any number.

lee1210
Apr 9, 2008, 08:17 AM
Thank you. But what I actually want to know is, why I need "strlen(Aszkeys[i])" 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