I am trying to find a user input word inside a string vector using binary search. But it always returns a positive integer. The vector is sorted and read from a txt file. The file looks like.
aah
aal
aas
and so on.
int binarySearchString(vector<string> arr, string x, int n) {
int lower = 0;
int upper = n - 1;
while (lower <= upper) {
int mid = lower + (upper - lower) / 2;
int res;
if (x == (arr[mid]))
res = 0;
if (res == 0)
return mid;
if (x > (arr[mid]))
lower = mid + 1;
else
upper = mid - 1;
}
return -1;
}