Compare a string with quotation marks

The function strcmp does not seem to like quotation marks in a string. I'm using GNU C++ 5.x. Did I miss something?

printf("%s\r\n", mystring ); // Output: "00"

if ( strcmp( mystring, "\"00\"" ) == 0 )
{
...
}

FYI...I wrote my own function that works just fine.

BOOL StrMatch(char *str, char *match)
{
while (*match)
{
if (!*str) return 0;
if (toupper(*str++)!=toupper(*match++)) return 0;
}
return 1; // no differences found
}
Last edited on
strcmp() does not care if either string contains quotes, so you must be doing something else wrong.

Your StrMatch() is not reflective. There are values of a and b for which StrMatch(a, b) != StrMatch(b, a). For example, StrMatch("aa", "a") == 1.
something is up. This simple program gives the expected output of 0 means equal from strcmp:

0
"00"

1
2
3
4
5
6
   int main()
   {
	 char c1[] =  "\"00\"";
	 char c2[] =  "\"00\"";
	 cout << strcmp(c1,c2) << endl << c1 << endl;	 
   }


using cygwin's g++ v6.4 which is newer but this sort of code should work all the way back to version 1.0

as noted your code is not always correct and it is also less efficient. A modern strcmp can dump the string as blocks of 64, even 128 bits at a time to the registers for comparison, rather than 1 byte at a time, among other tweaks (it can treat them as blocks of integers).
Last edited on
Topic archived. No new replies allowed.