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!

Proper string comparison

Status
Not open for further replies.

btaber

Programmer
May 26, 2002
307
US
I am current using strcmp(_strupr(argv[1]),"test"), but is considered unsafe, and is suggested I use _strupr_s. What would be the proper code to use _strupr_s, because strcmp expects char, and _strupr_s returns errno_t...
 
Why not use std::string for all your strings, and leave those unsafe C-style strings (and the various compiler specific attempts to make them safe) for C programs.

> strcmp(_strupr(argv[1]),"test")
Erm, you convert it to upper case, then compare it with something lower case?


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Sorry about the lower case... just putting a string in there... I am not much of a C programmer, how would I use std::string?
 
Code:
#include <string>  // For STL Strings.

using namespace std;

...

string str( "Hello" );
str += " World.";
cout << str << endl;  // Prints "Hello World."
Check the MSDN for all the other STL string methods...
 
> I am not much of a C programmer, how would I use std::string?
By clarifying whether you're programming in C or C++.

std::string is a C++ only thing.

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
It is written in C (I think), trying to check command line params:

Code:
char szListStores[12];
strcpy_s(szListStores,12,"/LISTSTORES");

if(argc==2)
{
	if(0!=strcmp(_strupr(argv[1]),szListStores))
		return -1;
	ListStores();
	printf("\n");
}

I get warnings that it is unsafe to use _strupr and I should use _strupr_s, but strcmp expects char *, not errno_t...

 
> but strcmp expects char *, not errno_t...
OK, so call them separately and not super-compressed into one line of code.

[tt]_strupr_s(argv[1]);
strcmp(argv[1],szListStores);[/tt]

Yes, this looks like C.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
I can't believe how simple that was... I didn't realize _strupr_s changed the original value, I thought it returned the uppercase value.... as they say, RTFM!

THANKS!!!

 
Try _stricmp() non-standard library function (from <string.h>/<cstring>) for case-insensitive comparison:
Code:
if (_stricmp(argv[1],"test"))
{
   /* It's not a "TEST", "Test" etc... */
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top