compare if 2 string types are equal

This is a discussion on compare if 2 string types are equal within the C++ Programming forums, part of the General Programming Boards category; hi,i got 2 variables of string format.how to compare if they are equal???? Code: string a="Asd"; string b="Asd"; if(strcmp(a,b)==0){ cout<<"true"; ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    24

    compare if 2 string types are equal

    hi,i got 2 variables of string format.how to compare if they are equal????

    Code:
            string a="Asd";
    	string b="Asd";
    	if(strcmp(a,b)==0){
    		cout<<"true";
    	}
    it gives me error.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > if(strcmp(a,b)==0){
    It's even easier.
    Code:
    	if (a == b){

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    A long(er) way (using the string container's compare member function):
    Code:
    string a="Asd";
    string b="Asd";
    if( a.compare(b) == 0 ) {
        cout<<"true";
    }
    Another long way (using the strcmp function):
    Code:
    string a="Asd";
    string b="Asd";
    if(strcmp(a.c_str(),b.c_str())==0){
        cout<<"true";
    }
    But yeah, just using operator== is short and sweet.

    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    strcmp() is only for c-style strings. like char*string_thing. the class string is a C++ construct and will not work with functions that require a char*.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> the class string is a C++ construct and will not work with functions that require a char*.
    Technically it can work if you use the c_str() member function, but in most cases there are better alternatives that are specific to the C++ string class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  3. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  4. Help Writing My Own String Compare
    By djwicks in forum C Programming
    Replies: 4
    Last Post: 04-07-2005, 09:44 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21