-1

In this exercise from C++ Primer strings chapter, the directions read:

Write a program to read two strings and report whether the strings are equal. If not, report which of the two is larger. Now, change the program to report whether the strings have the same length, and if not, report which is longer.

To which I wrote:

#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;


/* Write a program to read two strings and report whether the
strings are equal. If not, report which of the two is larger. Now, change
the program to report whether the strings have the same length, and if
not, report which is longer. */


int main()
{
    string line;
    string word;
    getline(cin, line);
    getline(cin, word);

    if (line == word) {
        cout << "String are equal." << endl;
    }
    else {
        if (line > word)
            cout << line << " is longer." << endl;
        else {
            cout << word << " is longer." << endl;
        }

    }

    return 0;
}

Which seemed to work for the problem. Now for the first example of:

Write a program to read two strings and report whether the strings are equal. If not, report which of the two is larger.

I changed the comparisons to have .size(), and for this one:

Write a program to read two strings and report whether the strings are equal. If not, report which of the two is larger.

I removed the .size() for the comparisons. I printed out both size and .length() and I got the same answers, so I was wondering if I am misinterpreting this problem or was it to show that length and size are really the same thing?

xyz
  • 393
  • 2
  • 12
  • 3
    "larger" = `strcmp` (or whatever the C++ equivalent is), "longer" = `strlen` (or whatever the C++ equivalent is) – Sean Bright Apr 07 '15 at 21:29
  • `std::string::size()` and `std::string::length()` are the same. – juanchopanza Apr 07 '15 at 21:30
  • So was that really what this question was trying to teach? – xyz Apr 07 '15 at 21:30
  • I am self-learning from this book at the moment. I must have mistyped the header question and construed my true intent as what I am ultimately trying to figure out is if the question calls for another piece of code for one of its parts, or if it expects the same answer for both. – xyz Apr 07 '15 at 21:36
  • 1
    Your posted program uses the word ***longer*** when it should be ***larger***, since it is telling you which string follows after the other one alphabetically. For the second exercise, all the comparisons should use either the `size()` method or the `length()` method (they are equivalent), and now the word is ***longer***, since it is finding the string that has more characters that the other one. – jxh Apr 07 '15 at 21:41
  • @jxh Thank you. That is exactly what I wanted to know, and now I understand. – xyz Apr 07 '15 at 21:46

1 Answers1

4

Both string::size and string::length are synonyms and return the exact same value. Reference http://www.cplusplus.com/reference/string/string/length/