Results 1 to 10 of 10

Thread: [C++]Comparing Strings Using Equality Operators.

  1. #1
    Join Date
    Dec 2008
    Beans
    180
    Distro
    Ubuntu 16.04 Xenial Xerus

    [C++]Comparing Strings Using Equality Operators.

    I'm reading an example and I am trying to think about how this would make sense. It's probably something simple, I just am a little confused.I'm trying to figure out if one of my strings in greater than the other using the method above. The program compiles and runs, but I don't understand the result I am getting.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(){
        string first, second;
        
        cin >> first >> second;
        
        if(first > second)
        cout << "Correct." << endl;
        else
        cout << "Incorrect." << endl;
        
        system("Pause");
        return 0;
    }
    My input was "country" and "applesauce" respectively.

    The output is saying "Correct." but I don't understand how "country" is greater than "applesauce." I've tested it with other words as well and sometimes the shorter word is greater and sometimes the larger word is greater. How is it determined?

    Thank you for reading.
    Last edited by Carpentr; December 15th, 2011 at 07:25 AM.
    AMD FX-9590 4.7Ghz, 256GB SSD, 2TB 7200RPM, nVidia 970 GTX, 16GB DDR3

  2. #2
    Join Date
    Jul 2009
    Beans
    109
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: [C++]Comparing Strings Using Equality Operators.

    My modification should give you the hint.
    I commented you system function since neither iostream or string should define it and try it with capital Letters.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(){
        string first, second;
        
        cin >> first >> second;
    
        cout << first << endl << second << endl;
    
        cout << (int) first[0] << '\t'+first << endl;
        cout << (int) second[0] << '\t'+second << endl;
        
        if(first > second)
        cout << "Correct." << endl;
        else
        cout << "Incorrect." << endl;
        
        //system("Pause");
        return 0;
    }
    Windows DOS not compute...

  3. #3
    Join Date
    May 2010
    Location
    uk
    Beans
    9,249
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: [C++]Comparing Strings Using Equality Operators.

    Hi

    It does not compare the length of the string (what i think you were expecting) but the value of the characters of the string.

    c > a in string comparison.

    Kind regards
    If you believe everything you read, you better not read. ~ Japanese Proverb

    If you don't read the newspaper, you're uninformed. If you read the newspaper, you're mis-informed. - Mark Twain

    Thinking about becoming an Ubuntu Member?

  4. #4
    Join Date
    May 2008
    Location
    UK
    Beans
    1,451
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: [C++]Comparing Strings Using Equality Operators.

    Quote Originally Posted by Carpentr View Post
    I'm reading an example and I am trying to think about how this would make sense. It's probably something simple, I just am a little confused.I'm trying to figure out if one of my strings in greater than the other using the method above. The program compiles and runs, but I don't understand the result I am getting.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(){
        string first, second;
        
        cin >> first >> second;
        
        if(first > second)
        cout << "Correct." << endl;
        else
        cout << "Incorrect." << endl;
        
        system("Pause");
        return 0;
    }
    My input was "country" and "applesauce" respectively.

    The output is saying "Correct." but I don't understand how "country" is greater than "applesauce." I've tested it with other words as well and sometimes the shorter word is greater and sometimes the larger word is greater. How is it determined?

    Thank you for reading.
    The comparison operators are looking at dictionary order (or strictly speaking character set ordering) rather than string length so :

    country > applesauce when you think about the order of the words in an dictionary.

    Similarly :
    cat > boat

    and in extreme
    zebra > aardvark

    but also :
    zebra > Zebra
    since Upper case letters come before lower case in the character Sequence.

    and also :
    "1" > "one" - numeric digit characters come before letters characters.
    "2" > "twenty" - for the same reason.
    Last edited by Tony Flury; December 15th, 2011 at 12:10 PM.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  5. #5
    Join Date
    Jun 2007
    Location
    Malvern, UK
    Beans
    992
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [C++]Comparing Strings Using Equality Operators.

    Just to add to the excellent replies, if you want to compare how long the strings are use the size() method on the strings.

  6. #6
    Join Date
    Feb 2007
    Location
    St. Louis, MO
    Beans
    4,930
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: [C++]Comparing Strings Using Equality Operators.

    Quote Originally Posted by Carpentr View Post
    I'm reading an example and I am trying to think about how this would make sense. It's probably something simple, I just am a little confused.I'm trying to figure out if one of my strings in greater than the other using the method above. The program compiles and runs, but I don't understand the result I am getting.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(){
        string first, second;
        
        cin >> first >> second;
        
        if(first > second)
        cout << "Correct." << endl;
        else
        cout << "Incorrect." << endl;
        
        system("Pause");
        return 0;
    }
    My input was "country" and "applesauce" respectively.

    The output is saying "Correct." but I don't understand how "country" is greater than "applesauce." I've tested it with other words as well and sometimes the shorter word is greater and sometimes the larger word is greater. How is it determined?

    Thank you for reading.
    Can someone define to me a String being greater than another String???

    I can see if one String has a greater length than another.
    Windows, only good for gaming.

  7. #7
    Join Date
    Dec 2008
    Location
    Deep Woods of PA
    Beans
    699
    Distro
    Kubuntu 11.10 Oneiric Ocelot

    Re: [C++]Comparing Strings Using Equality Operators.

    Quote Originally Posted by stchman View Post
    Can someone define to me a String being greater than another String???

    I can see if one String has a greater length than another.
    If you do a character by character comparison of the strings. Something like this:

    Code:
    for(int i = 0 ; i < max(len(str1), len(str2)) ; ++i)
    {
        if(str1[i] != str2[i])
            return (str1[i] - str2[i]);
        if( str1[i] = '\0' )
            return 0;
    }
    If this function returns value < 0 then str1 < str2, if value > 0 then str1 > str2, otherwise they are equal.
    Regards,

    Karlson

  8. #8
    Join Date
    Dec 2008
    Beans
    180
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: [C++]Comparing Strings Using Equality Operators.

    Thanks a lot for the replies everyone; it is appreciated.

    If you compare both strings without specifying a particular character then it will always determine the value by the first character of the string (string[0])?
    AMD FX-9590 4.7Ghz, 256GB SSD, 2TB 7200RPM, nVidia 970 GTX, 16GB DDR3

  9. #9
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: [C++]Comparing Strings Using Equality Operators.

    Quote Originally Posted by Carpentr View Post
    Thanks a lot for the replies everyone; it is appreciated.

    If you compare both strings without specifying a particular character then it will always determine the value by the first character of the string (string[0])?
    It runs down both strings, comparing the characters until it finds two that don't' match and then it returns the difference. Like good old strcmp() in C, which you can find written as:

    Code:
    int strcmp(const char *s1, const char *s2)
    {
      int ret = 0;
    
      while (!(ret = *(unsigned char *) s1 - *(unsigned char *) s2) && *s2) ++s1, ++s2;
    
      if (ret < 0)
        ret = -1;
      else if (ret > 0)
        ret = 1 ;
    
      return ret;
    }

  10. #10
    Join Date
    Dec 2008
    Location
    Deep Woods of PA
    Beans
    699
    Distro
    Kubuntu 11.10 Oneiric Ocelot

    Re: [C++]Comparing Strings Using Equality Operators.

    Quote Originally Posted by Carpentr View Post
    Thanks a lot for the replies everyone; it is appreciated.

    If you compare both strings without specifying a particular character then it will always determine the value by the first character of the string (string[0])?
    Not sure I understand the question. In C a string is an array of characters with terminating character being '\0'. In the comparison function the comparison will start from the first character in that array which is string[0], but it may not be the first character in the original string.
    Regards,

    Karlson

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •