Correct me if I am wrong, but binary search is used on sorted arrays.
You are trying to perform binary search on array of chars _ @ .
.
In terms of ASCII codes, that looks like this: [32][95][32][64][32][46]
.
So, when you call your function:
Low: 0 - Middle: 2 - High: 5
Seach: 46 - Actual: 32
Low: 3
Low: 3 - Middle: 4 - High: 5
Seach: 46 - Actual: 32
Low: 5
Low: 5 - Middle: 5 - High: 5
Found!!!
true
However, when you take out the empty spaces and run it over "_@."
Low: 0 - Middle: 1 - High: 2
Seach: 46 - Actual: 64
High: 0
Low: 0 - Middle: 0 - High: 0
Seach: 46 - Actual: 95
High: -1
Not found!!!
false
To summarize:
If you are absolutely sure you'll be running your search over a sorted array, then this is OK.
However, like in your case, if you can't guarantee that you'll have sorted array, then don't use binary search.