tags:

views:

33

answers:

2

How to declare and compare the strings?

// test ="my test string";

char testDest[256];
char *p= _com_util::ConvertBSTRToString(URL->bstrVal);
strcpy(testDest, p);

How can I compare test with testDest?

+1  A: 

Use strcmp. But I would suggest you go through a book on C programming first.

dirkgently
A: 

For non case sensitive comparisons try int strcmp ( const char * str1, const char * str2 ) http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

If you want the string comparing to be case sensitive, i.e. "test" != "TEsT", use int memcmp ( const void * ptr1, const void * ptr2, size_t num ). http://www.cplusplus.com/reference/clibrary/cstring/memcmp/

So: typedef char string[]; string sz1 = "This is a test string." string sz2 = "This is a test string." string sz3 = "This Is A Test String." if(memcmp(sz1, sz2, strlen(sz1) > strlen(sz2) ? strlen(sz1) : strlen(sz2)) == 0) printf("sz1 and sz2 are equal"); else printf("sz1 and sz2 are not equal"); if(memcmp(sz1, sz3, strlen(sz1) > strlen(sz2) ? strlen(sz1) : strlen(sz2)) == 0) printf("sz1 and sz3 are equal"); else printf("sz1 and sz3 are not equal"); if(strcmp(sz2, sz3) == 0) printf("sz2 and sz3 are equal"); else printf("sz2 and sz3 are not equal");

EDIT: You can also use stricmp() for case sensitive comparisons.

muntoo
strcmp is "exact", if by "exact", you mean case sensitive. The case-insensitive variant is called stricmp.
Jim Brissom
`stricmp` does not exist in the C99 Standard Library. You mean POSIX perhaps.
dirkgently