strcmpi

  • Thread starter Eduardo Elias Camponez
  • Start date
E

Eduardo Elias Camponez

Hi
Im doing a C code using GCC.
I tried to compare (case Incensitive) two strings.. but it seems dont
"know" strcmpi.. Im using 2 headers string.h and stdio.h
How can I do this?

Ex:
#include <stdio.h>
#include <string.h>

int main (void)
{
char * str = "Case";
char * str1 = "cAsE";

if (strcmpi(str,str1)==0)
printf("Same\n");
else
printf("Sorry\n");
}

Tks

Eduardo Elias Camponez
 
A

Allan Bruce

Eduardo Elias Camponez said:
Hi
Im doing a C code using GCC.
I tried to compare (case Incensitive) two strings.. but it seems dont
"know" strcmpi.. Im using 2 headers string.h and stdio.h
How can I do this?

Ex:
#include <stdio.h>
#include <string.h>

int main (void)
{
char * str = "Case";
char * str1 = "cAsE";

if (strcmpi(str,str1)==0)
printf("Same\n");
else
printf("Sorry\n");
}

Tks

Eduardo Elias Camponez

I believe strcmpi is an implementation-defined header. Try looking for it
in the man pages. Under VC++ it is called using _strcmpi() so may be
similar for your case.
Allan
 
K

keanu

Eduardo Elias Camponez wrote in
Hi
Im doing a C code using GCC.
I tried to compare (case Incensitive) two strings.. but it seems dont
"know" strcmpi.. Im using 2 headers string.h and stdio.h
How can I do this?

you probably have
strcasecmp()
 
J

j

Eduardo Elias Camponez said:
Hi
Im doing a C code using GCC.
I tried to compare (case Incensitive) two strings.. but it seems dont
"know" strcmpi.. Im using 2 headers string.h and stdio.h
How can I do this?

Ex:
#include <stdio.h>
#include <string.h>

int main (void)
{
char * str = "Case";
char * str1 = "cAsE";

if (strcmpi(str,str1)==0)
printf("Same\n");
else
printf("Sorry\n");
}

Tks

Eduardo Elias Camponez

Just provide your own version of ``strcmpi'' .. although I think you might
want to rename it, since str* is reserved by the implementation if I am not
mistaken.

----------------------------
#include <ctype.h>

int stricmp(char *s1, char *s2)
{
for (; *s1 && *s2 && (toupper(*s1) == toupper(*s2)); ++s1, ++s2);

return *s1 - *s2;
}
 
S

Simon Biber

j said:
Just provide your own version of ``strcmpi'' .. although I think you
might want to rename it, since str* is reserved by the implementation
if I am not mistaken.

Yes, it is.

int my_stricmp(const char *s1, const char *s2)

The parameters should be pointers to const char, for consistency with
the strcmp function.
{
for (; *s1 && *s2 && (toupper(*s1) == toupper(*s2)); ++s1, ++s2);

for (; *s1 && *s2 && (toupper((unsigned char)*s1) ==
toupper((unsigned char)*s2)); ++s1, ++s2);

You must convert the 'char' argument to 'unsigned char' to ensure that
there can be no negative values passed to the toupper function. The C
standard requires that the input to toupper() must be in the range of
'unsigned char'.
return *s1 - *s2;

There is no guarantee that the subtraction of two arbitrary 'char's
will be within the range of an int. On a very weird implementation
this could cause overflow problems.
 
I

Irrwahn Grausewitz

Allan Bruce said:
I believe strcmpi is an implementation-defined header. Try looking for it
in the man pages. Under VC++ it is called using _strcmpi() so may be
similar for your case.
Allan
The OP didn't mention a header called strcmpi.
Neither the strcmpi() nor _strcmpi() functions are standard C.

Irrwahn
 
I

Irrwahn Grausewitz

j said:
Just provide your own version of ``strcmpi'' .. although I think you might
want to rename it, since str* is reserved by the implementation if I am not
mistaken.
To be exact, ISO/IEC 9899:1999 states:

7.26.11 String handling <string.h>
Function names that begin with str, mem, or wcs and a lowercase
letter may be added to the declarations in the <string.h> header.

A perfectly valid name would be e.g.: strCmpi
----------------------------
#include <ctype.h>

int stricmp(char *s1, char *s2)
{
for (; *s1 && *s2 && (toupper(*s1) == toupper(*s2)); ++s1, ++s2);

return *s1 - *s2;
}

That's another possibility.

Regards

Irrwahn
 
A

Allan Bruce

it

oops, I meant function!
The OP didn't mention a header called strcmpi.
Neither the strcmpi() nor _strcmpi() functions are standard C.

I know, I was merely trying to indicate a possible area for the OP to look
at
 
M

Micah Cowan

Hi
Im doing a C code using GCC.
I tried to compare (case Incensitive) two strings.. but it seems dont
"know" strcmpi.. Im using 2 headers string.h and stdio.h
How can I do this?

Ex:
#include <stdio.h>
#include <string.h>

int main (void)
{
char * str = "Case";
char * str1 = "cAsE";

if (strcmpi(str,str1)==0)
printf("Same\n");
else
printf("Sorry\n");
}

There is no standard C library function for performing
case-insensitive comparisons against two strings. (OT:) If you
are willing to limit portability to POSIX platforms and systems
which provide the function as an extension, you may use
strcasecmp().

-Micah
 
S

Steve Zimmerman

Eduardo said:
Hi
Im doing a C code using GCC.
I tried to compare (case Incensitive) two strings.. but it seems dont
"know" strcmpi.. Im using 2 headers string.h and stdio.h
How can I do this?

Ex:
#include <stdio.h>
#include <string.h>

int main (void)
{
char * str = "Case";
char * str1 = "cAsE";

if (strcmpi(str,str1)==0)
printf("Same\n");
else
printf("Sorry\n");
}

Tks

Eduardo Elias Camponez

Thank you for your post, Eduardo.

#include <stdio.h>
#include <string.h>

int main()
{
char * str = "Case";
char * str1 = "cAsE";

/* strcmp returns 0 if the two strings are identical */

if (!strcmp(str, str1))
printf("Same\n");
else
printf("Sorry\n");

return 0;
}

--Steve
 
P

Peter Nilsson

Irrwahn Grausewitz said:
To be exact, ISO/IEC 9899:1999 states:

7.26.11 String handling <string.h>
Function names that begin with str, mem, or wcs and a lowercase
letter may be added to the declarations in the <string.h> header.

A perfectly valid name would be e.g.: strCmpi

Under C90, external identifiers need not be distinguishable by case,
and C90 implementations are only required to recognise external
identifiers up to 6 significant characters (IIRC).

So, strCmpi can theoretically have the same signature as strcmp,
STRCMP, StrCMP, StrCmp, etc...
 
I

Irrwahn Grausewitz

Steve Zimmerman said:
Thank you for your post, Eduardo.

#include <stdio.h>
#include <string.h>

int main()
{
char * str = "Case";
char * str1 = "cAsE";

/* strcmp returns 0 if the two strings are identical */

if (!strcmp(str, str1))
printf("Same\n");
else
printf("Sorry\n");

return 0;
}
Steve,

how does this code compare two strings _case insensitive_?

Regards

Irrwahn
 
I

Irrwahn Grausewitz

Under C90, external identifiers need not be distinguishable by case,
and C90 implementations are only required to recognise external
identifiers up to 6 significant characters (IIRC).

So, strCmpi can theoretically have the same signature as strcmp,
STRCMP, StrCMP, StrCmp, etc...

That's why I was referring to C99... :)

Irrwahn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,079
Messages
2,570,575
Members
47,207
Latest member
HelenaCani

Latest Threads

Top