What is wrong with the following code? How come it does not found the letter using my implementation of a binary search?
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
#include <cwctype>
using namespace std;
bool contains(string s, char a){
int m = 0;
int n = s.length()-1;
while (m != n) {
int k = (m + n) / 2;
if (s[k] == a)
return true;
if (s[k] < a) {
n = k - 1;
} else {
m=k + 1;
}
}
return false;
}
int main() {
string s = "miyvarxarmaiko";
char a = 'm';
if (contains(s,a) == true) {
cout << "s contains character a" << endl;
} else {
cout << "does not contain" << endl;
}
return 0;
}