Thread: Rules to compare two strings object

  1. #1
    Registered User
    Join Date
    Aug 2014
    Location
    Italy
    Posts
    5

    Rules to compare two strings object

    In my book I have the following example :
    Code:
        std::string str = "Hello"; 
        std::string phrase = "Hello world"; 
        std::string slang = "Hiya";
    and i have these two rules to compare 2 strings object :

    if two strings have different lenghts and if every character in the shorter string is equal to the corresponding character
    of the longer string, than the shorter string is less than the longer string.

    if any characters at corresponding positions of two strings differ, then the result of the string comparision is the result
    of comparing the first character at wich the strings differ

    then my book says : if we apply the rules of the comparision we know that phrase is greater than str( ok i've understood this ) and that slang is greater than both slang and phrase ( why ?? )

    could you exolain me rule number two ? in phrase and slang the characters differ and the first character that differ is not H so why my book says slang is bigger than phrase ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think you made a typo error, i.e., "slang is greater than both slang and phrase" should be "slang is greater than both str and phrase".

    Consider str and slang: their lengths are different, and not "every character in the shorter string is equal to the corresponding character of the longer string", hence we apply the second rule. The first character at which the strings differ are the characters at index 1, i.e., 'e' for str and 'i' for slang. Assuming ASCII or some other character set in which the letters of the English alphabet have values in their canonical order, 'e' < 'i', hence str < slang. Likewise for phrase and slang.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2014
    Location
    Italy
    Posts
    5
    Quote Originally Posted by laserlight View Post
    I think you made a typo error, i.e., "slang is greater than both slang and phrase" should be "slang is greater than both str and phrase".

    Consider str and slang: their lengths are different, and not "every character in the shorter string is equal to the corresponding character of the longer string", hence we apply the second rule. The first character at which the strings differ are the characters at index 1, i.e., 'e' for str and 'i' for slang. Assuming ASCII or some other character set in which the letters of the English alphabet have values in their canonical order, 'e' < 'i', hence str < slang. Likewise for phrase and slang.
    so i should the compare the characters of two strings object ? why if i change the value of str to "Hhllo" str is not greater than slang ? 'h' is bigger than 'i' right ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by piero borrelli
    so i should the compare the characters of two strings object ?
    Yes.

    Quote Originally Posted by piero borrelli
    why if i change the value of str to "Hhllo" str is not greater than slang ? 'h' is bigger than 'i' right ?
    Compile and run this program to find out:
    Code:
    #include <iostream>
    
    int main()
    {
        std::cout << static_cast<int>('h') << ' ' << static_cast<int>('i') << std::endl;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare strings:
    By jocdrew21 in forum C++ Programming
    Replies: 11
    Last Post: 01-05-2014, 07:23 PM
  2. How to compare strings in c++
    By Sanscorp1999 in forum C++ Programming
    Replies: 3
    Last Post: 08-12-2012, 04:03 PM
  3. compare two strings
    By cable in forum C Programming
    Replies: 3
    Last Post: 02-07-2012, 09:12 AM
  4. How can I compare strings?
    By errigour in forum C Programming
    Replies: 9
    Last Post: 11-16-2010, 05:54 PM
  5. Compare Two Strings?
    By Paul22000 in forum C++ Programming
    Replies: 9
    Last Post: 05-24-2008, 05:09 PM