A couple of problems I see:
In StringList::search
, the line
if (str[i] >= s)
should be changed to
if (str[i] == s)
You want to find an exact match, not the first lexicographically "greater" string, correct?
Next, the first line in StringList::remove
should use
numberOfStrings - 1
instead of just
numberOfStrings
If numberOfStrings = 3
, then you want to search in indices 0, 1, 2
, not 0, 1, 2, 3
.
However, instead of changing the parameter in the first line, you could also change (in the function StringList::search
) the line
for (int i = start; i <= end; i++)
to
for (int i = start; i < end; i++)
With these fixes, your algorithm should work.
The reason you would try to remove "Brown" and get "Cat" removed is because of the lexicographic "greater than" operation in the searching method. When you gave it "Brown", it would see "Cat" and say, hey! "Cat" > "Brown"! Let's return the index of "Cat"! And then the remove method would, well, remove "Cat"...