0

This is what I have right now (str is a dynamic, null-terminating char array):

bool String::operator<(const String& rhs) const {
    if (str == rhs.str)
        return false;
    int i = 0;
    while (str[i] != '\0' && rhs[i] != '\0') {
        if (str[i] > rhs[i])
            return false;
        ++i;
    }
    return true;
}

This passes most tests, but it fails on:

String s1("abc");
String s2("abcde");

assert(!(s2 < s1));

No matter how I alter the function it always seem to fail one test or another. How would YOU overload this operator? Basically I just need to compare two null-terminating char arrays and see which one is the lesser (without any libraries).

conjenks
  • 409
  • 1
  • 6
  • 18
  • Hint: You need an extra test before returning `true`. (Just because one of the strings at the end doesn't mean `*this` is less than `rhs`) – user786653 Oct 15 '16 at 19:37
  • 1
    Checkout the written answer in [Operator overloading](http://stackoverflow.com/questions/4421706/operator-overloading?rq=1s) – sudo_coffee Oct 15 '16 at 19:38

2 Answers2

2

You can take advantage of null-terminated strings to simplify the basic algorithm to:

  1. While the nth character of both strings are the same (incrementing n starting with 0):
  2. If the nth character of both strings is '\0' the strings are obviously the same, otherwise:
  3. Otherwise, compare the nth characters as unsigned values, to determine the result of the comparison.
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • Are those first two steps doing something different than what I do with `if (str == rhs.str) return false;` ? – conjenks Oct 15 '16 at 19:45
  • Yes, they are. Does `while (str[i] != '\0' && rhs[i] != '\0') ` mean "while the nth character of both strings are the same" to you? – Sam Varshavchik Oct 15 '16 at 19:46
1

If you change your loop to:

int i = 0;
while (true) {
    char l = str[i];
    char r = rhs.str[i++];
    if( l < r ) return true;
    if( l == 0 || l > r ) return false;
}

it should work. Note if you need to handle national alphabets properly, that usually has values > 127, you need to change l and r type to unsigned char

But easier solution would be:

return strcmp( str, rhs.str ) < 0;
Slava
  • 43,454
  • 1
  • 47
  • 90
  • I just ran the tests and that does indeed work perfectly, but my class has not gotten to the `unsigned` keyword yet and I don't think the professor would approve of that solution (unfortunately). But I appreciate it! Is there a way to change that to not use `unsigned`? Because honestly I don't even know what that is in the first place – conjenks Oct 15 '16 at 19:51
  • if you do not use ASCII > 127 for your strings you may change them to `char`. But algo will break if you put symbols with negative value for signed char. – Slava Oct 15 '16 at 19:52
  • I take it back, it should work with signed `char` as well, changed answer – Slava Oct 15 '16 at 19:58
  • I changed them to just 'char' and it still passes all tests, so I will roll with it for now. Thank you. – conjenks Oct 15 '16 at 19:58