how to compare strings

i'd love to know how to compare two strings to see if they are the same. is there an inbuilt function that can do this? i tried (string1==string2), but it didn't work. any help would be appreciated
string1 == string2 will work just fine, assuming you are using actual strings (ie: std::string) and not char arrays.

If you are using char arrays, you can use the strcmp function:

1
2
3
4
5
6
7
8
9
10
11
string a = "foo";
string b = "foo";

if(a == b) // works just fine
{}

char c[] = "foo";
char d[] = "foo";

if( strcmp(c,d) == 0 ) // the way to do it with char arrays.
{}
Last edited on
then, your definition about string is probably wrong... does your string1 and string2 is the type of char *? if it does, then no wonder your code is wrong...

if both is the type of string, the comparison should be, and will be true (assuming both have the same value)
Topic archived. No new replies allowed.