barlyee
barlyee

Reputation: 429

How to compare char/string?

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?

Upvotes: 0

Views: 4788

Answers (2)

Mateen Ulhaq
Mateen Ulhaq

Reputation: 27201

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.

Upvotes: 0

dirkgently
dirkgently

Reputation: 111120

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

Upvotes: 1

Related Questions