Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with stricmp function..

Status
Not open for further replies.

VBDotNetProgrammer

Programmer
Jul 9, 2003
50
GB
Hi all,

Im having a problem with the stricmp() function. Ive been using it in VC6 and it is recognised, however I am getting this error under GCC.

error: `stricmp` undeclared (first use this function)

Does this just mean that the stricmp function does not exist in the unix version of the string library?

im using #include <string> just as in windows.

Any ideas how i can rectify this?

Thanks
 
I've seen something like this defined in a few source files out there:
Code:
#ifndef WIN32 
  #define stricmp strcasecmp 
  #define strnicmp strncasecmp 
#endif
That defines stricmp as the function strcasecmp, which is apparently equivalent.

Also, sometimes on Unix I've had better luck doing #include <string.h> rather than #include <string>
 
The stricmp() function is non-standard, so it troubles...
 
Hi,

Thanks for that.

Do any of you know where i can look up only standard C++ libraries etc?

Thanks
 
Another quick question, what is the best way to do caseless string comparisons under GCC?
 
No, in no circumstance...
Case insensitive comparison is a common program case.
As usually, every C/C++ has its own stricmp() equivalent (see your compiler manual).
If you want to write portable codes (or can't find this equivalent), write your own case-insensitive comparator function (it's not so hard). For example (only, it's not 3 Mach code;):
Code:
#include <cctype>
// ASCII + ordered nationals only...
int cmpistr(const char* s1, const char* s2)
{
	int	c1, c2;
	int	cmp = 0;

	if (s1 && s2)
	for (;;)
	{
		c1 = *s1++;
		c2 = *s2++;
		if (c1 && c2)
		{
			c1 = tolower(c1)&0xFF; // 8 bits
			c2 = tolower(c2)&0xFF; // only
			if (c1 < c2)
			{
				cmp = -1;
				break;
			}
			else if (c1 > c2)
			{
				cmp = 1;
				break;
			}
		}
		else
		{
			if (c1)
				cmp = 1;
			else if (c2)
				cmp = -1;
			break;
		}
	}
	return cmp;
}
Be careful: strcmp (and stricmp) returns < 0 (not exactly -1) or > 0 (not exactly 1)...
 
VBDotNetProgrammer said:
Another quick question, what is the best way to do caseless string comparisons under GCC?

The compiler doesn't matter; the library does. However, if you're using GCC, then chances are you're also using GLibC, in which case this statement would apply:

oppcos said:
That defines stricmp as the function strcasecmp, which is apparently equivalent.


Also, heed the advice about header files. <string> declares the C++ [tt]string[/tt] class; <string.h> declares functions for dealing with C-style strings. Make sure you know which one you want. You used the wrong one in your initial post.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top