Thread: How are strings compared exactly?!??!

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    16

    Question How are strings compared exactly?!??!

    I know the syntax but I want to know the background work.

    If I have 2 strings. Here's what I think.

    1. the longer string is always the greater (>) one.
    2. if 2 are of same size then check element by element, first string with an element smaller than the other, is the smaller string.

    is this true?
    are there more to this?

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    It depends on what you are using to compare. There are many different comparison functions, and people often write their own to make it do what the want. You can just write a simple program to try it with the comparison function you are using.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    16
    ok then how do you compare 'c' strings be default?

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Again, there are lots of functions. Most people use strcmp I guess. I couldn't find the answer in the documentation for strcmp, so I wrote a little program to try it:
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <set>
    
    void OutputCompare(const std::string& s1, const std::string& s2);
    
    int main()
    {
        std::string string1 = "Ha";
        std::string string2 = "Hallo";
        std::string string3 = "He";
        std::string string4 = "Hello";
        std::string string5 = "Hi";
        std::string string6 = "Hillo";
    
        std::cout << "strcmp sort:" << std::endl;
        OutputCompare(string1, string2);
        OutputCompare(string1, string3);
        OutputCompare(string1, string4);
        OutputCompare(string1, string5);
        OutputCompare(string1, string6);
        OutputCompare(string2, string3);
        OutputCompare(string2, string4);
        OutputCompare(string2, string5);
        OutputCompare(string2, string6);
        OutputCompare(string3, string4);
        OutputCompare(string3, string5);
        OutputCompare(string3, string6);
        OutputCompare(string4, string5);
        OutputCompare(string4, string6);
        OutputCompare(string5, string6);
    
        std::set<std::string> wordSet;
        wordSet.insert(string1);
        wordSet.insert(string2);
        wordSet.insert(string3);
        wordSet.insert(string4);
        wordSet.insert(string5);
        wordSet.insert(string6);
    
        std::cout << "\nSet sort:" << std::endl;
        std::set<std::string>::const_iterator iterString = wordSet.begin();
        std::set<std::string>::const_iterator iterEnd = wordSet.end();
        for (; iterString != iterEnd; ++iterString)
            std::cout << *iterString << std::endl;
    }
    
    void OutputCompare(const std::string& s1, const std::string& s2)
    {
        if (strcmp(s1.c_str(), s2.c_str()) < 0)
            std::cout << std::setw(5) << s1 << " is less than " << s2 << std::endl;
        else if (strcmp(s1.c_str(), s2.c_str()) > 0)
            std::cout << std::setw(5) << s2 << " is less than " << s1 << std::endl;
        else
            std::cout << std::setw(5) << s1 << " is equivalent to " << s2 << std::endl;
    }
    Note the order of the strings in the code is the order they are sorted with strcmp, and even though I used std::string, I still compared c-style strings by using the .c_str() function.

    So the longer string is NOT always the greater one with this compare function. It just goes one letter at a time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM