Rules to compare two strings object

This is a discussion on Rules to compare two strings object within the C++ Programming forums, part of the General Programming Boards category; In my book I have the following example : Code: std::string str = "Hello"; std::string phrase = "Hello world"; std::string ...

  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
    22,303
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,303
    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 05: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, 06:09 PM

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